C# 为什么我的本地数据库(sql server)没有';我没有任何数据
我使用的是MVC dotnet框架。我有客户模型 详情如下:C# 为什么我的本地数据库(sql server)没有';我没有任何数据,c#,sql,asp.net,.net,sql-server,C#,Sql,Asp.net,.net,Sql Server,我使用的是MVC dotnet框架。我有客户模型 详情如下: Model Before using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Data.Entity; using System.Linq; using System.Web;
Model Before
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace WebApplication5.Models
{
public class CustomerContext : DbContext
{
public CustomerContext() : base("CustomerDB")
{
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<CustomerContext>());
}
public DbSet<Customer> Customers { get; set; }
}
public class Customer
{
public int id { get; set;}
[Required]
[StringLength(255)]
public String Name { get; set;}
public bool IsSubscribedToNewsletter { get; set;}
public MembershipType MembershipType { get; set;}
public byte MembershipTypeId { get; set; }
}
}
Model After
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data.Entity;
using System.Linq;
using System.Web;
using WebApplication5.Configurations;
namespace WebApplication5.Models
{
public class CustomerContext : DbContext
{
public CustomerContext() : base("CustomerContext")
{
Database.SetInitializer<CustomerContext>(new CustomerDBInitializer());
}
public DbSet<Customer> Customers { get; set; }
}
public class Customer
{
public int id { get; set;}
[Required]
[StringLength(255)]
public String Name { get; set;}
public bool IsSubscribedToNewsletter { get; set;}
}
}
之后,我运行了迁移,创建了本地数据库(Sql Server)和客户表。
然后,我添加了另一个文件,该文件将数据种子植入数据库:
我在PackageManager中运行了migrations update命令,但我的customers表仍然没有显示任何数据,并且是空的。请帮忙
更新的种子数据文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebApplication5.Models;
using WebApplication5.Repositories.CustomersRepositories;
namespace WebApplication5
{
public class CustomerDBInitializer : DropCreateDatabaseAlways<CustomerContext>
{
protected override void Seed(CustomerContext context)
{
IList<Customer> customers = new List<Customer>
{
new Customer { id = 1, Name = "Jojo", IsSubscribedToNewsletter = true }
};
var customer1 = new Customer { id = 1, Name = "Jojo", IsSubscribedToNewsletter = true };
context.Customers.AddRange(customers);
base.Seed(context);
}
} }
[my customer table is still empty][1]
[1]: https://i.stack.imgur.com/PJoLm.png
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用WebApplication5.Models;
使用WebApplication5.Repositories.CustomerRepositories;
命名空间WebApplication5
{
公共类CustomerDBInitializer:DropCreateDatabaseAlways
{
受保护的覆盖无效种子(CustomerContext上下文)
{
IList客户=新列表
{
新客户{id=1,Name=“Jojo”,IsSubscribedToNewsletter=true}
};
var customer1=新客户{id=1,Name=“Jojo”,IsSubscribedToNewsletter=true};
context.Customers.AddRange(Customers);
种子(上下文);
}
} }
[我的客户表仍然为空][1]
[1]: https://i.stack.imgur.com/PJoLm.png
-------------------------------------使用.NET EF6---------------------------------------------------------
public CustomerContext() : base("CustomerContext")
{
}
在运行“Enable Migrations”时生成的“Configuration.cs”中,种子方法是添加代码以填充数据库的地方
受保护的覆盖无效种子(WebApplication1.CustomerContext上下文)
{
IList客户=新列表
{
新客户{id=1,Name=“Jojo”,IsSubscribedToNewsletter=true},
新客户{id=2,Name=“Jojo”,IsSubscribedToNewsletter=true}
};
context.Customers.AddRange(Customers);
种子(上下文);
}
--------------------------------------------------使用.NETEFCore---------------------------------------------------------
我认为你的CustomerContext是错误的,试试这个。要在执行迁移时添加数据,请将此数据添加到OnModelCreating方法
public CustomerContext() : base()
{
}
public CustomerContext(DbContextOptions<CustomerContext> options) : base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
string connectionString = "Server=localhost; Initial Catalog=TestCustomers;persist security info=True; Integrated Security = SSPI;";
optionsBuilder.UseSqlServer(connectionString);
}
public DbSet<Customer> Customers { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Customer>().HasData(
new Customer { id = 1, Name = "Jojo", IsSubscribedToNewsletter = true}
);
}
公共CustomerContext():base()
{
}
公共CustomerContext(DbContextOptions选项):基本(选项)
{
}
配置时受保护的覆盖无效(DBContextOptions Builder Options Builder)
{
string connectionString=“Server=localhost;Initial Catalog=TestCustomers;persist security info=True;Integrated security=SSPI;”;
optionsBuilder.UseSqlServer(connectionString);
}
公共数据库集客户{get;set;}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
基于模型创建(modelBuilder);
modelBuilder.Entity().HasData(
新客户{id=1,Name=“Jojo”,IsSubscribedToNewsletter=true}
);
}
-------------------------------------使用.NET EF6---------------------------------------------------------
public CustomerContext() : base("CustomerContext")
{
}
在运行“Enable Migrations”时生成的“Configuration.cs”中,种子方法是添加代码以填充数据库的地方
受保护的覆盖无效种子(WebApplication1.CustomerContext上下文)
{
IList客户=新列表
{
新客户{id=1,Name=“Jojo”,IsSubscribedToNewsletter=true},
新客户{id=2,Name=“Jojo”,IsSubscribedToNewsletter=true}
};
context.Customers.AddRange(Customers);
种子(上下文);
}
--------------------------------------------------使用.NETEFCore---------------------------------------------------------
我认为你的CustomerContext是错误的,试试这个。要在执行迁移时添加数据,请将此数据添加到OnModelCreating方法
public CustomerContext() : base()
{
}
public CustomerContext(DbContextOptions<CustomerContext> options) : base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
string connectionString = "Server=localhost; Initial Catalog=TestCustomers;persist security info=True; Integrated Security = SSPI;";
optionsBuilder.UseSqlServer(connectionString);
}
public DbSet<Customer> Customers { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Customer>().HasData(
new Customer { id = 1, Name = "Jojo", IsSubscribedToNewsletter = true}
);
}
公共CustomerContext():base()
{
}
公共CustomerContext(DbContextOptions选项):基本(选项)
{
}
配置时受保护的覆盖无效(DBContextOptions Builder Options Builder)
{
string connectionString=“Server=localhost;Initial Catalog=TestCustomers;persist security info=True;Integrated security=SSPI;”;
optionsBuilder.UseSqlServer(connectionString);
}
公共数据库集客户{get;set;}
模型创建时受保护的覆盖无效(ModelBuilder ModelBuilder)
{
基于模型创建(modelBuilder);
modelBuilder.Entity().HasData(
新客户{id=1,Name=“Jojo”,IsSubscribedToNewsletter=true}
);
}
在何处以及如何调用SeedData()?检查连接字符串,并确保它使用的文件与添加数据的文件相同。您可以在计算机上搜索和检查MDF文件。在何处以及如何调用SeedData()?检查连接字符串,并确保它使用的是您添加数据到的同一文件。您可以在计算机上搜索并检查MDF文件。您好,谢谢您的回复,但由于我尝试了这么多错误,“CustomerContext”不包含“MembershipTypes”的定义,并且找不到可访问的扩展方法“MembershipTypes”,它接受“CustomerContext”类型的第一个参数(是否缺少using指令或程序集引用?)“CustomerContext.OnConfiguring(DbContextOptions Builder)”:未找到可重写“CustomerContext.OnModelCreating(ModelBuilder)”的合适方法:未找到可重写的合适方法,并且找不到类型或命名空间名称“DbContextOptions Builder”(您是否缺少using指令或程序集?找不到类型或命名空间名称“ModelBuilder”(您是否缺少using指令或程序集引用?)第一个原因是为了测试代码,我从customer类中删除了“MembershipTypes”类,正因为如此,我在
public CustomerContext() : base()
{
}
public CustomerContext(DbContextOptions<CustomerContext> options) : base(options)
{
}
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
string connectionString = "Server=localhost; Initial Catalog=TestCustomers;persist security info=True; Integrated Security = SSPI;";
optionsBuilder.UseSqlServer(connectionString);
}
public DbSet<Customer> Customers { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Customer>().HasData(
new Customer { id = 1, Name = "Jojo", IsSubscribedToNewsletter = true}
);
}