简体   繁体   中英

MVC 5 C# web application

Can somebody clarify those things:

  1. I have created my tables in a sql server.
  2. Created project in VS, and changed the connectionstring in web.config to point out to my db. It works for adding a user, and I can see the user info in my User table. But it does not work for creating an item forexample a Book item. The result is I got new table inserted in my db called Books, but I already have a table for Book, it should be stored in Book not in Books?
  3. So please what I am missing or doing wrong?

I have also added a context file called DefaultConnection.cs with the following content:

using System;
    using System.Collections.Generic;
    using System.Data.Entity;
    using System.Linq;
    using System.Web;

    namespace Example.Models
    {
        public class DefaultConnection : DbContext
        {
            // You can add custom code to this file. Changes will not be overwritten.
            // enter code here**`strong text`**
            // If you want Entity Framework to drop and regenerate your database
            // automatically whenever you change your model schema, please use data migrations.
            // For more information refer to the documentation:
            // http://msdn.microsoft.com/en-us/data/jj591621.aspx

            public DefaultConnection()
                : base("name=DefaultConnection")
            {
            }

            public System.Data.Entity.DbSet<Exaple.Models.Book> Books { get; set; }


        }
    }

`

another option if you want yo use another DbContext, is add the attribute Table to the class Book:

[Table("Book")]
public class Book {....}

Regards,

As I mentioned in the comment, You need to define your Books in ApplicationDbContext . you don't need to create another context:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{

        public DbSet<Book> Books { get; set; }
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM