使用说明搬运
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/