Entity Framework Complex Type property as key

c# entity-framework entity-framework-6

Question

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

1
1
9/12/2016 2:21:40 PM

Popular Answer

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; }
0
9/12/2016 2:27:34 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow