开发者

How to create references in Entity Framework when the database schema does not have them?

开发者 https://www.devze.com 2023-04-12 12:15 出处:网络
I have a database schema which I cannot alter, and which has tables that reference each other without declaring an explicit foreign key relationship.For example:

I have a database schema which I cannot alter, and which has tables that reference each other without declaring an explicit foreign key relationship. For example:

create table Foo (
  Id int 开发者_JAVA技巧identity not null primary key, 
  X int
)

create table Bar (
  Id int identity not null primary key, 
  FooId int, -- as if "references Foo(Id)"
  Y int
)

I would like to access these tables via EntityFramework (4.0 or 4.1). Is it possible to have EF treat FooId as it were a foreign key referencing the table Foo?


Yes, this is possible. For references about mapping using DB, Model, or Code First, I would suggest the ADO.NET team blog.


I posted this question on the Microsoft EF forum, and it was answered there; see http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/d4db23e4-ab0d-4d10-bd52-123cb2b641e1

In short I did not realize how much one can edit the associations after creating them.

0

精彩评论

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