开发者

How can I change StoreGeneratedPattern and force Entity Framework to generate insert statement with identity

开发者 https://www.devze.com 2023-03-28 23:32 出处:网络
I have server an开发者_JAVA百科d client database where I need to keep some of the server data in sync with client dataabse. Database schema is same apart from having IDENTITY(1,1) on server.

I have server an开发者_JAVA百科d client database where I need to keep some of the server data in sync with client dataabse. Database schema is same apart from having IDENTITY(1,1) on server.

Data can be created on server only. It has to be inserted on client with using server's id.

CREATE TABLE [MyServer].[dbo].[Test1](
[Test1Id] [int] IDENTITY(1,1) NOT NULL,
[Test1Value] [datetime] NOT NULL,
CONSTRAINT [PK_Test1] PRIMARY KEY CLUSTERED ( [Test1Id] ASC ) ) ON [PRIMARY]

CREATE TABLE [MyClient].[dbo].[Test1](
[Test1Id] [int] NOT NULL,
[Test1Value] [datetime] NOT NULL,
CONSTRAINT [PK_Test1] PRIMARY KEY CLUSTERED ( [Test1Id] ASC ) ) ON [PRIMARY]

Is there any way to change StoreGeneratedPattern for entity classes without rebuilding assembly so I can insert IDENTITY on client side? I spent some time looking into

EntityModelCodeGenerator

but without any success.

Of course I run server and client in different app domain so I can do this change on startup.

I am using default EntityFramework classes. I can change to POCO entities if it resolves my problem. Classes are simple and referenced by id's instead of NavigationProperties.

Thanks


I found one solution which will enable me to run client and server from same solution but it is VERY hacky and I will try to avoid. I will be better of changing int's to GUID's generated in code and there fore removing StoreGeneratedPattern="Identity" server as well.

Solution would be to have a post build event on my CLIENTs project which does:

  1. Removes StoreGeneratedPattern="Identity" from my EDMX file on targeted classes.
  2. Build project containing EDMX file.
  3. Copy dll's to CLIENTs project.
  4. Restoring original server dll and edmx files.

Is there any better solution? You see how far I am willing to go so GUID's seem like a better solution for me. Le me know if I am missing something.


My final solution is to remove StoreGeneratedPattern="Identity" on both sides (Client/Server) and generate object Id in a code before inserting into DB. This way my code can stay client/server agnostic and only creates ID for default value.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号