如何在 asp mvc 中使用一个数据库

how to use one database in asp mvc

public class project
{
    public int id { get; set; }
    public string ProjectName { get; set; }
    public string Description { get; set; }
    public string Category { get; set; }
    public int UserprojectId { get; set;  }
    public int? SponserId { get; set; }
    public decimal Cost { get; set; }
    public DateTime CreatedTime { get; set; }
    public DateTime EstimateTime { get; set; }

}
public class projectDB : DbContext
{
    public DbSet<project> projects { get; set; }
}

和这个模型

public class sponser
{
    public int id { get; set; }
    public string FirstName { get; set; }
    public string lastName { get; set; }
    public string CompanyName { get; set; }
    public string UserName { get; set; }
    public string Password { get; set; }

    public DateTime DateRegister { get; set; }
    public String CompanyPhone { get; set; }
    public string CellPhone { get; set; }
    public string Email { get; set;  }
    public bool EmailConfirmation { get; set; }
    public string Country { get; set; } 
    public string City { get; set; }
    public string PostalCode { get; set; }
    public String Address { get; set; }

}
public class sponserDB : DbContext
{
    public DbSet<sponser> sponsers { get; set; }
}

在我的网络配置文件中 我添加这个名字

<add name="SponseringDB"
  connectionString="Data Source=(localdb)\v11.0;Initial Catalog=sponseringDB;Integrated Security=True"
  providerName="System.Data.SqlServerCe.4.0"
/>

当 运行 这并为我创建一些记录时,它首先创建了两个数据库 sponseringDb,第二个是 projectDb 我想使用一个数据库。

需要有关实现此目标的建议。

你有两个 class 由 DbContext 继承,这就是创建 2 个数据库的原因,如果你只想拥有一个数据,即:projectDB

将您的 projectDB class 更改为

    public class projectDB : DbContext
    {

   public projectDB () : base("projectDBConnectionString") //<- name of your connection string
        {
        }
        public DbSet<project> projects { get; set; }
        public DbSet<sponser> sponsers { get; set; }
    }

并在您的网络配置中添加名为 'projectDBConnectionString' 的连接字符串,即:

<add name="projectDBConnectionString"
  connectionString="Data Source=(localdb)\v11.0;Initial Catalog=sponseringDB;Integrated Security=True"
  providerName="System.Data.SqlServerCe.4.0"
/>

现在您可以从 web.config 中删除连接字符串 'SponseringDB' 并删除 class 'sponserDB'

希望对您有所帮助