首页 新闻 会员 周边 捐助

EF的Database.SetInitializer()在EF Core中的替代实现是什么

0
悬赏园豆:30 [待解决问题]

在Entity Framewok中有个Database.SetInitializer(),在Entity Framework Core中没有,请问EF Core中的替代实现是什么?

dudu的主页 dudu | 高人七级 | 园豆:29759
提问于:2016-12-24 13:55
< >
分享
所有回答(2)
-1

使用说明搬运

SQLite

public class BloggingContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlite("Filename=./blog.db");
    }
}

MySQL

public class BlogContext:DbContext
    {
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseMySQL("server=10.255.19.111;database=mydb;uid=root;pwd=yourpassword;");
        }
        public DbSet<Blog> blog { get; set; }
        public DbSet<User> User { get; set; }
    }

SQLServer

public class BloggingContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
      optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=EFGetStarted.ConsoleApp.NewDb;Trusted_Connection=True;");
    }
}

如果是用控制文件的话

<?xml version="1.0" encoding="utf-8"?>
<configuration>

  <connectionStrings>
    <add name="BloggingDatabase"
         connectionString="Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" />
  </connectionStrings>
</configuration>

代码引用

public class BloggingContext : DbContext
{
    public DbSet<Blog> Blogs { get; set; }
    public DbSet<Post> Posts { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
      optionsBuilder.UseSqlServer(ConfigurationManager.ConnectionStrings["BloggingDatabase"].ConnectionString);
    }
}

 

https://docs.microsoft.com/en-us/ef/core/miscellaneous/configuring-dbcontext

https://docs.microsoft.com/en-us/ef/core/miscellaneous/connection-strings

https://docs.microsoft.com/en-us/ef/core/providers/oracle/

Smartree | 园豆:314 (菜鸟二级) | 2016-12-24 16:19
0

@dudu 请问你这个问题解决了,我现在也遇到类似的

jesn | 园豆:72 (初学一级) | 2018-07-25 17:03
支持(0) 反对(0) dudu | 园豆:29759 (高人七级) | 2018-07-25 17:25
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册