[英]Entity Framework Complex Type property as key
Is there a way in Entity Framework to use some complex type as key for an entity and map to existing database? 在Entity Framework中是否有一种方法可以使用某种复杂类型作为实体的键并映射到现有数据库?
Let say I have database like this: 假设我有这样的数据库:
create table people ( id int, name nvarchar(128) )
and I'd like to map the following C# class structure to this table: 我想将以下C#类结构映射到此表:
class PersonId
{
public int Id { get; set; }
}
class Person
{
public PersonId Id { get; set; }
public string Name { get; set; }
}
How could I do that? 我怎么能这样做?
-Dmytro -Dmytro
What's the point of having a separate PersonId
class if you only have on property? 如果你只拥有属性,拥有一个单独的
PersonId
类有什么意义? that seems like bad practice to me. 这对我来说似乎是不好的做法。
Perhaps all you need is a composite key. 也许您只需要一个复合键。 To use in in EF you can declare your class like this:
要在EF中使用,您可以像这样声明您的类:
[Key, Column(Order = 0)]
public int column1 { get; set; }
[Key, Column(Order = 1)]
public int column2 { get; set; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.