I'm trying to create a MVC3 application, i'm troubled with EF code first to create DB. I have this tables: User, Category, Product, Loan. A User can create none or more Categories. A User can add none or more Products. A User can add none or more Loans. A Category can have one or more Products. A Category belongs to a User. A Product can have none or more Loans. A Product belongs to a User. A Product is in a Category. A Loan belongs to a User. A Loan is added to a Product.
public class User
{
public int UserID { get; set; }
public string UserName { get; set; }
public virtual ICollection<Category> Categorys { get; set; }
public virtual ICollection<Product> Products { get; set; }
public virtual ICollection<Loan> Loans { get; set; }
}
public class Category
{
public int CategoryID { get; set; }
public string CategoryName { get; set; }
public int UserID { get; set; }
public virtual User User { get; set; }
public virtual ICollection<Product> Products { get; set; }
}
public class Product
{
public int ProductID { get; set; }
public string ProductName { get; set; }
public int UserID { get; set; }
public int CategoryID { get; set; }
public virtual User User { get; set; }
public virtual Category Category { get; set; }
public virtual ICollection<Loan> Loans { get; set; }
}
public class Loan
{
public int LoanID { get; set; }
public bool LoanStatus { get; set; }
public int UserID { get; set; }
public int ProductID { get; set; }
public virtual User User { get; set; }
public virtual Product Product { get; set; }
}
Have maded the context:
public class BuisnessContext : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Category> Categorys { get; set; }
public DbSet<Product> Products { get; set; }
public DbSet<Loan> Loans { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
Have added the connectionString:
<add name="BuisnessContext"
connectionString="Data Source=|DataDirectory|Buisness.sdf"
providerName="System.Data.SqlServerCe.4.0"/>
Also a have maded a simple Initializer class:
public class BuisnessInitializer : DropCreateDatabaseIfModelChanges<BuisnessContext>
{
protected override void Seed(BuisnessContext context)
{
var users = new List<User>
{
new User { UserName = "u1"},
new User { UserName = "u2"} };
users.ForEach(s => context.Users.Add(s));
context.SaveChanges();
var categories = new List<Category>
{
new Category { CategoryName = "N1", UserID=1 } };
categories.ForEach(s => context.Categorys.Add(s));
context.SaveChanges();
var products = new List<Product>
{
new Product { ProductName = "N1", UserID = 1, CategoryID = 1 }
};
products.ForEach(s => context.Products.Add(s));
context.SaveChanges();
var loans = new List<Loan>
{
new Loan { LoanStatus = true, UserID = 2, ProductID = 1 }
};
loans.ForEach(s => context.Loans.Add(s));
context.SaveChanges();
}
}
Also i have generate a controller for User to get the users, but when i try to get the Users i received an error like:
Model compatibility cannot be checked because the EdmMetadata type was not included in the model. Ensure that IncludeMetadataConvention has been added to the DbModelBuilder conventions.
I tried to change the Database.SetInitializer<BuisnessContext>(new BuisnessInitializer());
whith Database.SetInitializer<BuisnessContext>(null);
Then i geted the error that table User doesen't exist and i didn't find any table in my APP_DATA folder -> Buisness.mdf The database was created, but there was any table.
I understand that in my BuisnessContext i must to put some code for One to many or something like this, but i don't know how to do that.
Any help please!