如题
asp.net core +EF+MySql(code first fluentapi)
大概架构这样
然后我创建了通用的泛型类
在studentservice中
{
public int AddNew(string Name, int Age)
{
using (MyDbcontext ctx = new MyDbcontext())
{
BaseService<Student> stu = new BaseService<Student>(ctx);
Student s = new Student();
s.Name = Name;
s.Age = Age; ctx.Student.Add(s);
return ctx.SaveChanges();
}
}
}会报错如果在Mydbcontext 的类中加一个空的构造函数,
报错会消失,但是在插入数据时候
报错信息:System.InvalidOperationException:“No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.”
asp.net core +EF+MySql(code first fluentapi)
大概架构这样
然后我创建了通用的泛型类
在studentservice中
{
public int AddNew(string Name, int Age)
{
using (MyDbcontext ctx = new MyDbcontext())
{
BaseService<Student> stu = new BaseService<Student>(ctx);
Student s = new Student();
s.Name = Name;
s.Age = Age; ctx.Student.Add(s);
return ctx.SaveChanges();
}
}
}会报错如果在Mydbcontext 的类中加一个空的构造函数,
报错会消失,但是在插入数据时候
报错信息:System.InvalidOperationException:“No database provider has been configured for this DbContext. A provider can be configured by overriding the DbContext.OnConfiguring method or by using AddDbContext on the application service provider. If AddDbContext is used, then also ensure that your DbContext type accepts a DbContextOptions<TContext> object in its constructor and passes it to the base constructor for DbContext.”
一般来说。需要
public MyDbcontext()
: base("name=MyDbcontext")
{
}
通过base来默认寻找配置文件中MyDbcontext的连接字符串和数据库驱动。
要看你的 mydbcontext才知道。
要看你的 mydbcontext才知道。
public class MyDbcontext : DbContext
{
public MyDbcontext(DbContextOptions<MyDbcontext> options) : base(options)
{
} public DbSet<Student> Student { get; set; } public DbSet<Teacher> Teacher { get; set; } public DbSet<Class> Class { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder); //IConfiguration
//base.OnModelCreating(modelBuilder);
//modelBuilder.Configurations.AddFromAssembly(Assembly.GetExecutingAssembly()); var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
.Where(q => q.GetInterface(typeof(IEntityTypeConfiguration<>).FullName) != null); foreach (var type in typesToRegister)
{ dynamic configurationInstance = Activator.CreateInstance(type);
modelBuilder.ApplyConfiguration(configurationInstance);
}
}麻烦了
要看你的 mydbcontext才知道。
public class MyDbcontext : DbContext
{
public MyDbcontext(DbContextOptions<MyDbcontext> options) : base(options)
{
} public DbSet<Student> Student { get; set; } public DbSet<Teacher> Teacher { get; set; } public DbSet<Class> Class { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder); //IConfiguration
//base.OnModelCreating(modelBuilder);
//modelBuilder.Configurations.AddFromAssembly(Assembly.GetExecutingAssembly()); var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
.Where(q => q.GetInterface(typeof(IEntityTypeConfiguration<>).FullName) != null); foreach (var type in typesToRegister)
{ dynamic configurationInstance = Activator.CreateInstance(type);
modelBuilder.ApplyConfiguration(configurationInstance);
}
}麻烦了
2个方法
1 你调用 MyDbcontext 的有参构造方法
2 你按照我上面给你的代码,自己实现MyDbcontext无参构造函数,通过base指定默认调用父类的有参构造函数
要看你的 mydbcontext才知道。
public class MyDbcontext : DbContext
{
public MyDbcontext(DbContextOptions<MyDbcontext> options) : base(options)
{
} public DbSet<Student> Student { get; set; } public DbSet<Teacher> Teacher { get; set; } public DbSet<Class> Class { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder); //IConfiguration
//base.OnModelCreating(modelBuilder);
//modelBuilder.Configurations.AddFromAssembly(Assembly.GetExecutingAssembly()); var typesToRegister = Assembly.GetExecutingAssembly().GetTypes()
.Where(q => q.GetInterface(typeof(IEntityTypeConfiguration<>).FullName) != null); foreach (var type in typesToRegister)
{ dynamic configurationInstance = Activator.CreateInstance(type);
modelBuilder.ApplyConfiguration(configurationInstance);
}
}麻烦了
2个方法
1 你调用 MyDbcontext 的有参构造方法
2 你按照我上面给你的代码,自己实现MyDbcontext无参构造函数,通过base指定默认调用父类的有参构造函数大神你好,应该怎么改造呢?
public class studentservice:MyDbcontext{
}
public partial class leadmw2_efContext : DbContext
{
static leadmw2_efContext()
{
Database.SetInitializer<leadmw2_efContext>(null);
} public leadmw2_efContext()
: base("Name=leadmw2_efContext")
{
} public DbSet<MW_Actor> MW_Actor { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new MW_ActorMap());
}
}
您好
.net core 中的code first fluentapi
一般来说。需要
public MyDbcontext()
: base("name=MyDbcontext")
{
}
通过base来默认寻找配置文件中MyDbcontext的连接字符串和数据库驱动。