DbContext是否可以在构造函数中配置/传递连接字符串?
public partial class MyContext : DbContext
{
public MyContext() { }
public MyContext(DbContextOptions<MyContext> options) : base(options) { }
}
MyContext opt = new MyContext("con_str");
Run Code Online (Sandbox Code Playgroud)
而不是将其硬编码为:
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer(
@"Server=(localdb)\mssqllocaldb;Database=MyDB;Integrated Security=True");
}
Run Code Online (Sandbox Code Playgroud)
我该如何使用:
DbContextOptions<MyContext> opts;
Run Code Online (Sandbox Code Playgroud)
以下答案:将连接字符串传递给代码优先的 DbContext不起作用。
我尝试在我的项目中使用EF和MySQL.我补充说:
MySql.Data.EntityFrameworkCore
MySql.Data.EntityFrameworkCore.Design
进入我project.json的工具部分project.json是空的.当我跑:
Scaffold-DbContext "myconnectionstr" MySql.Data.EntityFrameworkCore -OutputDir Models -StartupProject "myproject"
Run Code Online (Sandbox Code Playgroud)
控制台显示错误:
无法在提供程序集MySql.Data.EntityFrameworkCore中找到名为DesignTimeProviderServicesAttribute的预期程序集属性
在现有项目中,我如何知道它是代码优先还是数据库优先?
项目有这几行代码:
public class TestDBContext : DbContext
{
public DbSet<Player> Players { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
Run Code Online (Sandbox Code Playgroud)
并且项目没有.edmx文件。如果需要任何其他细节,我会分享。
编辑:
Player.cs 类
public class Player
{
public int PlayerID { get; set; }
public string PlayerName { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
编辑 12.05.2017
如果我改变database name从connection string和运行项目,它创建database with the new name with all tables。可能这会被击中答案。
环境:
我正在使用数据库优先方法。我的数据库中有3个表。所有这些表都有状态字段以指示记录状态。这是一个整数字段。
场景:
我使用db first方法从该表创建了模型图。然后,我为CRUD操作创建了一个通用的存储库接口和类。
以下是界面;
public interface IGenericRepository<T> where T : class
{
IQueryable<T> GetAll();
Task<T> GetAsync(int id);
void Add(T entity);
void Delete(T entity);
void Edit(T entity);
Task<bool> SaveAsync();
}
Run Code Online (Sandbox Code Playgroud)
以下是通用存储库类
public abstract class GenericRepository<C, T> :
IGenericRepository<T> where T : class
where C : MyDBContext, new()
{
public virtual IQueryable<T> GetAll()
{
IQueryable<T> query = _entities.Set<T>();
return query;
}
//Removed other methods for clarity
}
Run Code Online (Sandbox Code Playgroud)
需求:
在GetAll方法中,我需要检查状态字段并仅返回value = 1
当前的解决方案我有:
由于这是通用存储库,因此无法访问方法中的字段。我们可以创建一个带有Status字段的基本Interface,然后在通用存储库中继承它,并可以在方法中使用此字段。
如下所示的更改;
public interface IGenericRepository<T> where T : …Run Code Online (Sandbox Code Playgroud) 我使用此代码来创建外键:
Create.ForeignKey("FK_Table1_Table2")
.FromTable("Table1").ForeignColumn("Table1_ID")
.ToTable("Table2").PrimaryColumn("Table2_ID");
Run Code Online (Sandbox Code Playgroud)
结果我有一对多的关系:
public class Table1
{
public int Table1_ID { get; set; }
public virtual Table2 Table2 { get; set; }
}
public class Table2
{
public Table2()
{
this.Tables1 = new HashSet<Table1>();
}
public int Table2_ID { get; set; }
public virtual ICollection<Table1> Tables1 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
但我想看到:
public class Table2
{
public int Table2_ID { get; set; }
public virtual Table1 Table1 { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
有没有办法使用 FluentMigrator 来做到这一点?
我正在使用现有系统并将其更新为 .NET Core、Web API 和 EF Core。
现有系统有2张表:
虽然ParentId存在于子表中,但没有外键引用,但我希望在查询父表时能够使用 include 。我要求不要添加 FK 作为删除的一部分,他们将 -ve 值放入parentId列中。通过这种方式,他们可以将其恢复,并以这种方式构建遗留系统。
现在,在数据库优先迁移中,如何在没有 fk 的情况下指定导航属性,以便我的 EF Core 发挥关系作用;或者至少将它们一起归还。添加可为空的外键不是一个选项,因为在添加 -ve 值时它会破坏系统。
我确实建议完全清理数据库并删除 -ve 值,但这涉及大量测试并且没有可交付成果。长话短说,如何在数据库首次迁移中添加没有外键的导航属性?
我尝试在模型中添加集合和虚拟条目,但迁移后它被覆盖。我根据本文档在模型构建器上使用 HasMany 添加了此内容 - https://learn.microsoft.com/en-us/ef/core/modeling/relationships?tabs= Fluent-api%2Cfluence-api-simple-key% 2C单键
但脚手架覆盖了我的导航属性
我看到有一些方法可以在 Entity Framework Core 2.0 中构建实体和数据库上下文。
Scaffold-DbContextdotnet ef dbcontext scaffold为什么有两种工具,有什么区别?
我一直在搜索 EF Core 文档,如果在实体映射上添加 .HasIndex() 会给 DbFirst 场景带来任何好处,我找不到任何东西。
我有这个 20yo 数据库,其中创建了所有必要的表和索引,我正在映射一些表以使用 EF Core 查询它们。我想知道,在永远不会通过代码更新表架构的 DbFirst 场景中映射索引有什么好处?它会影响 EF 生成 SQL 查询的方式吗?