Ethan Pelton
Ethan Pelton

Reputation: 1796

EDMX is overwriting Key data annotations

This applies to EF 5 and database first modeling. My model was built useing the EF generator from an existing DB.

I'm using the [Key] data annotation in my model classes because the primary key fields have names that are not in line with EF conventions.

Everything works, but when I open the root EDMX files, the model classes are updated and any manual changes I had made are lost.

Should I be making my changes in a different manner?

Upvotes: 1

Views: 735

Answers (2)

Ethan Pelton
Ethan Pelton

Reputation: 1796

the solution that worked for me was to overide the EF convention in OnModelCreating method in the context class.

protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

        modelBuilder.Entity<abk_Bookings>()
                    .HasKey(e => e.booking_number);}

Upvotes: 0

Daniel Dawes
Daniel Dawes

Reputation: 1005

You could update your T4 template to add in the data annotation for you on primary keys?

if (simpleProperties.Any())
{
    foreach (var edmProperty in simpleProperties)
    {
     if (ef.IsKey(edmProperty)){
#>
[Key]
<# } #>

Upvotes: 1

Related Questions