Note that there are some explanatory texts on larger screens.

plurals
  1. POcode first database model
    text
    copied!<p>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.</p> <pre><code>public class User { public int UserID { get; set; } public string UserName { get; set; } public virtual ICollection&lt;Category&gt; Categorys { get; set; } public virtual ICollection&lt;Product&gt; Products { get; set; } public virtual ICollection&lt;Loan&gt; 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&lt;Product&gt; 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&lt;Loan&gt; 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; } } </code></pre> <p>Have maded the context:</p> <pre><code>public class BuisnessContext : DbContext { public DbSet&lt;User&gt; Users { get; set; } public DbSet&lt;Category&gt; Categorys { get; set; } public DbSet&lt;Product&gt; Products { get; set; } public DbSet&lt;Loan&gt; Loans { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Conventions.Remove&lt;PluralizingTableNameConvention&gt;(); } } </code></pre> <p>Have added the connectionString:</p> <pre><code>&lt;add name="BuisnessContext" connectionString="Data Source=|DataDirectory|Buisness.sdf" providerName="System.Data.SqlServerCe.4.0"/&gt; </code></pre> <p>Also a have maded a simple Initializer class:</p> <pre><code>public class BuisnessInitializer : DropCreateDatabaseIfModelChanges&lt;BuisnessContext&gt; { protected override void Seed(BuisnessContext context) { var users = new List&lt;User&gt; { new User { UserName = "u1"}, new User { UserName = "u2"} }; users.ForEach(s =&gt; context.Users.Add(s)); context.SaveChanges(); var categories = new List&lt;Category&gt; { new Category { CategoryName = "N1", UserID=1 } }; categories.ForEach(s =&gt; context.Categorys.Add(s)); context.SaveChanges(); var products = new List&lt;Product&gt; { new Product { ProductName = "N1", UserID = 1, CategoryID = 1 } }; products.ForEach(s =&gt; context.Products.Add(s)); context.SaveChanges(); var loans = new List&lt;Loan&gt; { new Loan { LoanStatus = true, UserID = 2, ProductID = 1 } }; loans.ForEach(s =&gt; context.Loans.Add(s)); context.SaveChanges(); } } </code></pre> <p>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:</p> <blockquote> <p>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.</p> </blockquote> <p>I tried to change the <code>Database.SetInitializer&lt;BuisnessContext&gt;(new BuisnessInitializer());</code> whith <code>Database.SetInitializer&lt;BuisnessContext&gt;(null);</code></p> <p>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.</p> <p>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.</p> <p>Any help please!</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload