Is it possible to use composite key for Entity Framework with Model First?

Asked

Viewed 684 times

5

I have the modeling down and will need to map composite keys like the Embedded id of Hibernate, but in the Entity framework using the model first, it is possible ?

inserir a descrição da imagem aqui

  • Did you have to create the database, then import it to your project (to create the Models)? if yes, you tried to modify the models to specify for the Entity the compound Chavers?

1 answer

-3

Yes, as follows

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

    [Key, Column(Order = 1)]
    [Required(ErrorMessage = "A ActivityName is required")]
    [StringLength(50, ErrorMessage = "Activity Name must not exceed 50 characters")]
    public string ActivityName { get; set; }

}

Reference: https://stackoverflow.com/questions/5466374/composite-key-with-ef-4-1-code-first

  • But this solution is for code first and not model first.

  • You must have misexpressed yourself. In Entity, there are Empty Code First Model and Code First from database. Code first and model first, in this case, is the same thing! MSDN Model first: https://msdn.microsoft.com/en-us/data/jj205424.aspx

Browser other questions tagged

You are not signed in. Login or sign up in order to post.