Reputation: 1369
Just wondering if it was possible to add Data Annotations to a class referenced from a class library which has no reference on EntityFramework.
For example Project.Data.Entities
library
public class User {
public long Id { get; set; }
public string UserName { get; set; }
}
Project.Data.Repositories.EntityFramework
references Project.Data.Entities
library. How can I add the Data Annotations regarding Key properties, Column names, Table names, etc.?
Upvotes: 1
Views: 738
Reputation: 6501
There are fluent APIs for this purpose.
EDIT
About your mapping you have to override OnModelCreating
public class TestContext : DbContext
{
public DbSet<User> Users { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<User>()
.ToTable("user")
.HasKey(_ => _.Id);
modelBuilder.Entity<User>()
.Property(_ => _.Id).HasColumnName("id");
modelBuilder.Entity<User>()
.Property(_ => _.UserName).HasColumnName("username"); // Add also HasMaxLength here
}
}
(if your database already exists and it's not created by EF on your model you need to disable also migrations)
EDIT
If you installed SQL Server with a CI codepage, column name casing is not important. So you need only to specify HasMaxLength
Upvotes: 2