0

If I have the following model:

[Table("Person")]
public class PersonDao
{
    public Guid Id { get; set; }
    public ICollection<Address> { get; set; }
    // other properties
}

[Table("Address")]
public class AddressDao
{
    public Guid Id { get; set; }
    public PersonDao Person { get; set; }
    // other properties
}

Entity Framework uses Person and Address correctly for the table names but the foreign key in Address is called PersonDao_Id. I want it to be Person_Id.

Is this a bug or am I supposed to write a custom convention for the property names?

NOTE: I use MySQL with Entity Framework, I don't know if that matters.

EDIT: I know that I can specify the column name manually using the ForeignKey attribute or the fluent API. I need this to work automatically and globally.

Botond Balázs
  • 2,512
  • 1
  • 24
  • 34
  • http://stackoverflow.com/questions/11148662/mapping-a-foreign-key-with-a-custom-column-name might be usefull – Vladimir Dec 27 '16 at 14:41
  • [ForeignKey("Person_Id")] and details here : http://stackoverflow.com/questions/5082991/influencing-foreign-key-column-naming-in-ef-code-first-ctp5 – Mehmet Dec 27 '16 at 14:41

2 Answers2

2

Use attributes just like you did to have different names for the table and class:

[Table("Address")]
public class AddressDao
{
    public Guid Id { get; set; }

    [ForeignKey("Person_Id")] 
    public PersonDao Person { get; set; }
    // other properties
}

If you don't want to use the default convention you could just remove Dao from your class names:

public class Person
{
    public Guid Id { get; set; }
    public ICollection<Address> { get; set; }
    // other properties
}

public class Address
{
    public Guid Id { get; set; }
    public Person Person { get; set; }
    // other properties
}
D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • I'm sorry but my question wasn't clear enogh. I already know about this. I need this to work automatically and globally. – Botond Balázs Dec 27 '16 at 14:57
  • So you're OK with using attributes to specify a table name but not a column name? Why not just name your classes without the `"Dao"`? To answer the question I do not know of a way to globally override the default convention. – D Stanley Dec 27 '16 at 15:13
  • I like to name my data access classes `Dao` to differentiate them from my domain classes (which have a structure that cannot be directly mapped using EF). – Botond Balázs Dec 27 '16 at 15:17
  • The difference is that I think foreign key names should contain the table name (either generated from the class name of specified explicitly using an attribute or the fluent API) instead of the class name *by default*. I shouldn't need to specify them explicitly. I actually think that is a subtle bug in EF but I'm not sure. – Botond Balázs Dec 27 '16 at 15:19
0

If you want make your own name of column in database, you can use Fluent API in protected override void OnModelCreating(DbModelBuilder modelBuilder) method in your database context. Add to your DAO class properties with column name.

[Table("Person")]
public class PersonDao
{
    public Guid Id { get; set; }
    public ICollection<Address> Addresses { get; set; }
    // other properties
}

[Table("Address")]
public class AddressDao
{
    public Guid Id { get; set; }
    public Guid MyPersonDaoColumnName { get; set; }
    public PersonDao Person { get; set; }
    // other properties
}

and then in Fluent API write:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<AddressDao>().HasRequired(x => x.Person)
                                     .WithMany(x => x.Addresses)
                                     .HasForeignKey(x => x.MyPersonDaoColumnName);
}

but it is ugly to mix Fluent API with Attribute so you need also:

modelBuilder.Entity<AddressDao>.ToTable("Address");
modelBuilder.Entity<PersonDao>.ToTable("Person");
Jarosław Kończak
  • 3,387
  • 2
  • 19
  • 36
CrazyBaran
  • 572
  • 3
  • 20