簡體   English   中英

MVC 5 C#Web應用程序

[英]MVC 5 C# web application

有人可以澄清這些事情嗎?

  1. 我已經在SQL服務器中創建了表。
  2. 在VS中創建了項目,並在web.config中更改了連接字符串以指向我的數據庫。 它適用於添加用戶,並且可以在“用戶”表中看到該用戶信息。 但是它不適用於創建項目,例如書本項目。 結果是我在數據庫中插入了一個名為Books的新表,但是我已經有了Book的表,它應該存儲在Book中而不是在Books中嗎?
  3. 所以,請問我缺少或做錯了什么?

我還添加了一個名為DefaultConnection.cs的上下文文件,其內容如下:

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; }


        }
    }

`

如果要使用另一個DbContext,另一個選擇是將屬性Table添加到Book類中:

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

問候,

正如我在評論中提到的,您需要在ApplicationDbContext定義您的Books 您無需創建其他上下文:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM