Server-generated keys and server-generated values are not supported by SQL Server Compact

谁说胖子不能爱 提交于 2019-11-26 19:57:03

问题


I just started to play with the entity framework, so I decided to connect it to my existing SQL Server CE database. I have a table with an IDENTITY(1, 1) primary key but when I tried to add an entity, I've got the above-mentioned error.

From MS Technet artice I learned that

SQL Server Compact does not support entities with server-generated keys or values when it is used with the Entity Framework. When using the Entity Framework, an entity’s keys may be marked as server generated. This enables the database to generate a value for the key on insertion or entity creation. Additionally, zero or more properties of an entity may be marked as server-generated values. For more information, see the Store Generated Pattern topic in the Entity Framework documentation. SQL Server Compact does not support entities with server-generated keys or values when it is used with the Entity Framework, although the Entity Framework allows you to define entity types with server-generated keys or values. Data manipulation operation on an entity that has server-generated values throws a "Not supported" exception.

So now I have a few questions:

  • Why would you mark key as server-generated if it is not supported and will throw an exception? It's hard to make sence from the quoted paragraph.
  • When I've tried to add StoreGeneratedPattern="Identity" to my entity's property, Studio complained that it is not allowed. What I'm doing wrong?
  • What is the best workaround for this limitation (including switching to another DB)? My limitations are zero-installation and using entity framework.

回答1:


When I hit this limitation, I changed the type to uniqueidentifier




回答2:


Use uniqueidentifier or generate a bigint/int key value manually is your best option.

Something like this perhaps ...

    private static object lockObject = new object();

    private static long nextID = -1;

    public static long GetNextID()
    {
        lock (lockObject)
        {
            if (nextID == -1) nextID = DateTime.UtcNow.Ticks; else nextID++;
            return nextID;
        }
    }

This assumes that you don't generate more than one record per tick during an application run (plus the time to stop and restart). This is a reasonable assumption I believe, but if you want a totally bullet proof (but more complex) solution, go read the highest ID from the database and increment from that.




回答3:


SQL CE version 4.0 fixed this problem with its Entity Framework provider.




回答4:


I just hit this issue too... mostlytech's answer is probably the best option, GUIDs are very easy to use and the risk of key collision is very low (although not inexistant).

  • Why would you mark key as server-generated if it is not supported and will throw an exception? It's hard to make sence from the quoted paragraph.

Because SQL Server (not Compact) supports it, and other third parties may support it too... Entity Framework is not only for SQL Server Compact ;)




回答5:


In my case, all of my classes have the primary key named "ID"

I created an interface

public class IID
{
    public Int32 ID { get; set; }
}

Then I create an extension method

public static Int32 GetNextID<T>(this ObjectSet<T> objects)
    where T : class, IID
    {
        T entry = objects.OrderByDescending(u => u.ID).FirstOrDefault();
        if (entry == default(T))
            return 1;
        return entry.ID + 1;
    }

Then when I need a new ID, I just do this:

MyObject myobj = new MyObject();
myobj.ID = entities.MyTable.GetNextID();



回答6:


the other option is to use SqlCeResultSet on the tables that have the identity column.




回答7:


i have a primary key named ID with data type of INT32 and have Identity Column

Just do this

MyEntity Entity = new MyEntity();

String Command;

command = "Insert into Message(Created,Message,MsgType)values('12/1/2014','Hello World',5); Entity.ExecuteStoreCommand(command);

--Exclude the primary key in the insert Statement

--Since the SQLCE do not support system generated keys

--Do not use LINQ because it supplies a default value to 0 for Primary keys that has a data type of INT



来源:https://stackoverflow.com/questions/648449/server-generated-keys-and-server-generated-values-are-not-supported-by-sql-serve

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!