-3

So I have a list of an object using an unconventional name for EF, and within that object I am defining a reference to the upper level object that holds the list. Everything is all swell until I see the name it generates for me, I tried to change that using the [Column] attribute and that fails.

Code:

[Column("CompanyFK")]
public Company CompanyFK { get; set; }

Generated Code:

public override void Up()
  {
       AddColumn("dbo.AspNetUsers", "CompanyFK_CompanyPK", c => c.Int());
       CreateIndex("dbo.AspNetUsers", "CompanyFK_CompanyPK");
       AddForeignKey("dbo.AspNetUsers", "CompanyFK_CompanyPK", "dbo.Company", "CompanyPK");
  }
Bailey Miller
  • 1,376
  • 2
  • 20
  • 36

3 Answers3

0

In the call to AddColumn(), you are adding a column called "CompanyFK_CompanyPK".

If you want the field to be called "CompanyFK" (like your property name), then change your call to AddColumn to

   AddColumn("dbo.AspNetUsers", "CompanyFK", c => c.Int());

Now your property will match the column name, so you can remove the [Column] attribute.

Rick Hodder
  • 2,189
  • 3
  • 26
  • 54
  • I want the column to be named CompanyFK in the database. – Bailey Miller Aug 11 '17 at 15:01
  • I am just going to walk away from the Entity Framework for the rest of the day. It has pissed me off and something as simple as creating a 1:M linking with custom names doesn't want to work. I am going to work on something else for the day and I will come back to this later, your answer looks correct and I will mark it as correct if so later after a break. – Bailey Miller Aug 11 '17 at 15:56
  • OK. Taking a break from it is a good idea when you feel this way. We all go through this- I'm sure that you will figure it out. – Rick Hodder Aug 11 '17 at 16:02
  • @BaileyMiller - any luck? – Rick Hodder Aug 15 '17 at 18:56
  • I have actually had to go through a lot of junk but I eventually wiped my database, and I am slowly rebuilding it using the Fluent Api because it allows me to make the changes I want. – Bailey Miller Aug 16 '17 at 12:56
0

Use model builder to customize your mapping like in this answer:

Mapping a foreign key with a custom column name

I hope it helps,

Juan

Juan
  • 2,156
  • 18
  • 26
0

The Column attribute changes the mapping between the class' property and the table's column. It does not rename an existing column in the database.

Using code first will enable creating new tables, but by default it won't modify DB objects that you have already created.

So if you are using an existing table, you will need to rename the column yourself or write migration code.

Danny Varod
  • 17,324
  • 5
  • 69
  • 111