Click here to Skip to main content
15,897,360 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to specify Table schema using entity framework databse first approach
Posted

1 solution

You should create a configuration class based on your model.In this you can specify the behavior of that property
C#
public class SalesOrderConfiguration:EntityTypeConfiguration<salesorder>
    {
       public SalesOrderConfiguration()
       {
           Property(co => co.CustomerName).HasMaxLength(30).IsRequired();
           Property(co => co.PONumber).HasMaxLength(10).IsOptional();
       }
    }


Your model class will look like below
C#
public class SalesOrder
   {

       public int SalesOrderId { get; set; }
       public string CustomerName { get; set; }
       public string PONumber { get; set; }
   }


You can specify the configuration class in your DbContext
public class SalesContext:DbContext
   {
       public SalesContext()
           : base("DefaultConnection")
       {

       }

       public DbSet<salesorder> SalesOrders { get; set; }

       protected override void OnModelCreating(DbModelBuilder modelBuilder)
       {
           modelBuilder.Configurations.Add(new SalesOrderConfiguration());
       }
   }</salesorder>


Hope this helps
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900