简体   繁体   中英

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?

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:

    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

What's the point of having a separate PersonId class if you only have on property? 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:

[Key, Column(Order = 0)]
public int column1 { get; set; }

[Key, Column(Order = 1)]
public int column2 { get; set; }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM