Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying the name of a column in Entity Framework for a Referenced Object

I have a pre-defined database that I want to map using Entity Framework 4 CodeFirst.

public class Site
{
    public int SiteId { get; set; }
    public string SiteName { get; set; }
    public DateTime InstallDate { get; set; }
    public string Phase { get; set; }
    public string Address { get; set; }
    public string GpsPosition { get; set; }
    public string NetworkDetail { get; set; }
    public string SiteCode { get; set; }
    public string UserGroupCode { get; set; }
    public string InfrastructureNumber { get; set; }
    public string Province { get; set; }

    public virtual ICollection<LcuSetting> LcuSettings { get; set; }
}

And another class

public class LcuSetting
{
    public int LCUSettingId { get; set; }
    [Column(Name="Site_Id")]
    public Site Site { get; set; }


    public string Name { get; set; }
    public string IPAddress { get; set; }
    public string SubnetMask { get; set; }
    public string DefaultGateway { get; set; }
}

Because of the mapping conventions of EF4 it is looking for a column SiteSiteId in the table LCUSettings, which it cannot find since the Column is actually named Site_ID

In my DbContext derived class I override the OnModelCreating method and set the tables names to use.

modelBuilder.Entity<Site>().ToTable("Site");

this works fine.

When I try to specify the column name however, as follows

modelBuilder.Entity<LcuSetting>().Property(c => c.Site).HasColumnName("Site_Id");

I receive the following exception message

The type 'LcuSystemOnline.Models.Site' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Data.Entity.ModelConfiguration.Configuration.Types.StructuralTypeConfiguration.Property(System.Linq.Expressions.Expression>)'

I understand the exception, but how do I how get the modelBuilder to assign a specific ColumnName to the Site

like image 205
jvanrhyn Avatar asked Dec 10 '10 04:12

jvanrhyn


People also ask

How do I get column names in Entity Framework?

var names = typeof(User). GetProperties() . Select(property => property.Name) . ToArray();

How do I change a column name in fluent API?

Configure Column Name, Type and OrderThe HasColumnName() method is used to change the column name of the DateOfBirth property. Also, the HasColumnOrder() and HasColumnType() methods change the order and datatype of the corresponding column.

How do you call a table valued function in Entity Framework?

Step 1 − Select the Console Application from the middle pane and enter TableValuedFunctionDemo in the name field. Step 2 − In Server explorer right-click on your database. Step 3 − Select New Query and enter the following code in T-SQL editor to add a new table in your database.

Is column a data annotation attribute?

The Column attribute can be applied to one or more properties in an entity class to configure the corresponding column name, data type and order in a database table.


1 Answers

In CTP5, you cannot use the Column attribute to specify the foreign key name that we create. You instead have to do it this way with the fluent API:

public class Category
{
    public int Id { get; set; }
    public string Name { get; set; }
    public ICollection<Product> Products { get; set; }
}

public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Category Category { get; set; }
}

public class MyContext : DbContext
{
    public DbSet<Product> Products { get; set; }
    public DbSet<Category> Categories { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Product>()
            .HasRequired(p => p.Category)
            .WithMany(c => c.Products)
            .IsIndependent()
            .Map(mc => mc.MapKey(c => c.Id, "CategoryId")); 
    }
}

Note the call to "Map" in the OnModelCreating method. This is something that a number of people have bumped into and I like the idea of using the ColumnAttribute to help with the name.

You can see this blog article I wrote for more details: http://blogs.msdn.com/b/adonet/archive/2010/12/10/code-first-mapping-changes-in-ctp5.aspx

like image 181
Jeff Avatar answered Sep 28 '22 11:09

Jeff