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

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
What is the correct way to insert/update a row using Entity Framework? I know
We all know T-SQL's string manipulation capabilities sometimes leaves much to be desired... I
We all know how to use <ctrl>-R to reverse search through history, but did
We all know that RAW pointers need to be wrapped in some form of
We all know that commenting our code is an important part of coding style
We all know the various ways of testing OO systems. However, it looks like
We all know what virtual functions are in C++, but how are they implemented
We all know that having a good note taking tool is important as a

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.