Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4238508
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:54:50+00:00 2026-05-21T02:54:50+00:00

as all of us know, entity framework can’t hold geography data. So my idea

  • 0

as all of us know, entity framework can’t hold geography data. So my idea was, to specify the longitude and latitude as decimal in my model. After executing the SQL script for creating the tables I would start another script for adding a geography column. Then I would like to update this column on every INSERT or UPDATE (on longitude and latitude) by a trigger. Is the following trigger okay, or is it something bad? I’m asking because I’m not very familiar with trigger, but it works for now.


CREATE TRIGGER Update_Geography 
ON [People]
FOR INSERT, UPDATE
AS
BEGIN
    DECLARE @longitude DECIMAL(8, 5), @latitude DECIMAL(8, 5)

    SET @longitude = (SELECT ins.Location_Longitude FROM inserted ins)
    SET @latitude = (SELECT ins.Location_Latitude FROM inserted ins)

    IF (@longitude != 0 AND @latitude != 0)
    BEGIN
        UPDATE [People]
        SET
            Location_Geography = geography::STGeomFromText('POINT(' + CONVERT(VARCHAR(100),@longitude) + ' ' +  CONVERT(VARCHAR(100),@latitude) + ')',4326)
        WHERE
            Id = (SELECT ins.Id FROM inserted ins)
    END
END

Would be glad if someone could help me.

Regards

Edit:

script looks like that:


ALTER TABLE [People] ADD Location_Geography AS (
    CONVERT(GEOGRAPHY, CASE
        WHEN Location_Latitude  0 AND Location_Longitude  0 THEN
            geography::STGeomFromText('POINT(' + CONVERT(VARCHAR, Location_Longitude) + ' ' + CONVERT(VARCHAR, Location_Latitude) + ')',4326)
        ELSE
            NULL
    END
    )
)

works but can’t query that column :/
Thx

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T02:54:51+00:00Added an answer on May 21, 2026 at 2:54 am

    Try a PERSISTED COMPUTED column: http://msdn.microsoft.com/en-us/library/ms191250.aspx (might need an outer cast here)

    Location_Geography AS (
        CASE
            WHEN Location_Latitude <> 0 AND Location_Longitude <> 0 THEN
                geography::STGeomFromText('POINT(' + CONVERT(VARCHAR(100),Location_Longitude) + ' ' +  CONVERT(VARCHAR(100),Location_Latitude) + ')',4326)
            ELSE
            NULL
        END
    )
    

    This avoids having to make a trigger with pretty much the same overall effect.

    Triggers: http://msdn.microsoft.com/en-us/library/ms191524.aspx

    Your trigger could probably be modified as:

    CREATE TRIGGER Update_Geography 
    ON [People]
    FOR INSERT, UPDATE
    AS
    BEGIN
            UPDATE [People]
            SET
                Location_Geography = geography::STGeomFromText('POINT(' + CONVERT(VARCHAR(100),Location_Longitude) + ' ' +  CONVERT(VARCHAR(100),Location_Latitude) + ')',4326)
            WHERE (UPDATE(Location_Longitude) OR UPDATE(Location_Latitude))
                AND Id IN (SELECT ins.Id FROM inserted ins)
        END
    END
    

    Here’s an example showing both manual and calc’ed columns:

    IF EXISTS ( SELECT  *
                FROM    sys.objects
                WHERE   object_id = OBJECT_ID(N'[dbo].[SO5572806]')
                        AND type IN (N'U') ) 
        DROP TABLE [dbo].[SO5572806]
    GO
    
    CREATE TABLE SO5572806
        (
         lo DECIMAL(8, 5) NOT NULL
        ,la DECIMAL(8, 5) NOT NULL
        ,man GEOGRAPHY NULL
        ,calc AS (CONVERT(GEOGRAPHY, CASE WHEN la <> 0
                                            AND lo <> 0
                                       THEN GEOGRAPHY::STGeomFromText('POINT('
                                                                  + CONVERT(VARCHAR, lo)
                                                                  + ' '
                                                                  + CONVERT(VARCHAR, la)
                                                                  + ')', 4326)
                                       ELSE NULL
                                  END))
        )
    GO
    
    INSERT  INTO dbo.SO5572806
            (lo, la)
    VALUES  (0, 0),
            (-90, 30)
    
    UPDATE  dbo.SO5572806
    SET     man = GEOGRAPHY::STGeomFromText('POINT(' + CONVERT(VARCHAR, lo) + ' '
                                          + CONVERT(VARCHAR, la) + ')', 4326)
    WHERE   lo <> 0
            AND la <> 0
    
    SELECT  *
    FROM    dbo.SO5572806
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I share the auto-generated entity data model (generated object classes) amongst all
I know that I can use virtual keyword to tell the entity framework that
I'm brand new to the Entity Framework and trying to learn all it can
I don't know if I should test my @Entity-annotated Pojos. After all, there are
We all know you can't do the following because of ConcurrentModificationException : for (Object
I know this same question was asked for Entity Framework 1, but now that
I ran across this code in one of our Entity Framework applications. I know
When using the Entity Framework there are basically two ways to create your model.
Here's the scenario: ASP.NET MVC2 Web Application Entity Framework 4 (Pure POCO's, Custom Data
So I installed Entity Framework June CTP day before and all was fine until

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.