First, influence the name of foreign key columns in EF code (CTP5)

c# code-first entity-framework

Question

My POCO class shares an ancestor with another class with which it has two one-way unary links. The property names are not reflected in the names of the foreign keys in the produced schema. (The PersonId and PersonId1 field names are provided by the properties MainContact and FinancialContact).

How can I make the database column names generated by the schema creation match the property names?

The model appears as follows:

The class model

The code appears as follows:

public class CustomerContext: DbContext
{
   public DbSet<Organisation> Organisations { get; set; }
   public DbSet<Person> Persons { get; set; }

   protected override void OnModelCreating(ModelBuilder builder)
   {
      DbDatabase.SetInitializer(new DropCreateDatabaseAlways<CustomerContext>());
   }
}

public abstract class Customer
{
   public int Id { get; set; }
   public string Name { get; set; }
}

public class Person : Customer
{
   public string Email { get; set; }
}

public class Organisation : Customer
{
   public Person FinancialContact { get; set; }
   public Person MainContact { get; set; }
}

The schema appears as follows:enter image description here

Respondent druttka


It's good to know that this is a CTP5 problem since druttka's response below did the trick. In order to specify the cascade behavior for EF, I followed the example in the link provided by druttka and utilized the fluent API. Morteza Manavi has some more excellent reading for you here.

This is the current code:

public class CustomerContext : DbContext
{
   public DbSet<Organisation> Organisations { get; set; }
   public DbSet<Person> Persons { get; set; }

   protected override void OnModelCreating(ModelBuilder builder)
   {
      DbDatabase.SetInitializer(new DropCreateDatabaseAlways<CustomerContext>());

      builder.Entity<Organisation>()
         .HasRequired(p => p.MainContact)
         .WithMany()
         .HasForeignKey(p => p.MainContactId)
         .WillCascadeOnDelete(false);
      builder.Entity<Organisation>()
         .Property(p => p.MainContactId)
         .HasColumnName("MainContact");

      builder.Entity<Organisation>()
         .HasRequired(p => p.FinancialContact)
         .WithMany()
         .HasForeignKey(p => p.FinancialContactId)
         .WillCascadeOnDelete(false);
      builder.Entity<Organisation>()
         .Property(p => p.FinancialContactId)
         .HasColumnName("FinancialContact");
   }
}

public abstract class Customer
{
   public int Id { get; set; }
   public string Name { get; set; }
}

public class Person : Customer
{
   public string Email { get; set; }
}

public class Organisation : Customer
{
   public Person FinancialContact { get; set; }
   public int FinancialContactId { get; set; }

   public Person MainContact { get; set; }
   public int MainContactId { get; set; }
}

Which now provides the much better database:enter image description here

1
15
2/24/2011 8:45:51 AM

Accepted Answer

Convention is prioritized above configuration when using EF Code First. However, by overriding DbContent, you may provide specific alternatives. OnModelCreating. Many instances, thanks of ScottGu, here.

EDIT

As a result, MapSingleType was eliminated in CTP5 as stated in here. The following is applicable to basic string attributes, but not to relationships between your Organization and Person. I'm fascinated and will continue to look into it, but in the meanwhile, maybe this can help you get started or someone else can finish the response.

public class Person : Customer
{
    [Column(Name="EmailAddress")]
    public string Email { get; set; }
}

EDIT 2

Okay, I understand it now. I got the solution here. Disclaimer: My sole confirmation is that the database structure was generated according to plan. I have not verified that further CRUD operations or seeding data function as planned.

public class Organisation : Customer
{
    [Column(Name = "FinancialContact")]
    public int? FinancialContactId { get; set; }
    [ForeignKey("FinancialContactId")]
    public Person FinancialContact { get; set; }
    [Column(Name = "MainContact")]
    public int? MainContactId { get; set; }
    [ForeignKey("MainContactId")]
    public Person MainContact { get; set; }
}
10
2/24/2011 1:56:29 AM


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