我正在尝试学习来自经典ASP / VBScript背景的C#。
预先(以防万一有人可以在没有以下背景信息和代码的情况下回答) - 我的DbContext界面不允许我这样做:
_dbcontext.Entry(model).State = EntityState.Modified;
我试图使用Entry方法时出现以下错误:
'MyNamespace.Models.IMyDataContext'不包含'Entry'的定义,并且没有扩展方法'Entry'可以找到接受类型'MyNamespace.Models.IMyDataContext'的第一个参数(你是否缺少using指令或汇编参考?)
如何正确定义界面,使其包含DbContext类的Entry方法?
背景
我有人(据说)知道他们的东西帮助我根据我们从公共连接信息表中检索的数据获得以下代码设置以连接到MSSQL或MySQL。 MSSQL和MySQL中的模式对于数据模型是相同的。
public interface IMyDataContext
{
DbSet<MyModel> ModelData { get; set; }
}
public class dbMySQL : DbContext, IMyDataContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var table = modelBuilder.Entity<MyModel>().ToTable("tablename");
table.HasKey(t => t.Id);
table.Property(t => t.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
table.Property(t => t.Key);
table.Property(t => t.Value);
base.OnModelCreating(modelBuilder);
}
public dbMySQL(DbConnection existingConnection, boolcontextOwnsConnection) : base(existingConnection, contextOwnsConnection) { }
public DbSet<MyModel> ModelData { get; set; }
}
public class dbMSSQL : DbContext, IMyDataContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var table = modelBuilder.Entity<MyModel>().ToTable("tablename");
table.HasKey(t => t.Id);
table.Property(t => t.Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
table.Property(t => t.Key);
table.Property(t => t.Value);
base.OnModelCreating(modelBuilder);
}
public dbMSSQL(string connectionString) : base(connectionString) { }
public DbSet<MyModel> ModelData { get; set; }
}
使用上面的代码,我已经能够成功地从表中获取连接信息并返回DbContext,如下所示:
private IMyDataContext selectDbProvider(int Id)
{
// Get database connection info
var connInfo = _db.ConnModel.Find(Id);
string dbProvider = connInfo.dbType.ToString();
IMyDataContext _dbd;
if (dbProvider == "MySql.Data.MySqlClient")
{
var connectionStringBuilder = new MySqlConnectionStringBuilder();
connectionStringBuilder.Server = connInfo.dbServer;
connectionStringBuilder.UserID = connInfo.dbUser;
connectionStringBuilder.Password = connInfo.dbPassword;
connectionStringBuilder.Database = connInfo.dbName;
connectionStringBuilder.Port = 3306;
_mysqlconn = new MySqlConnection(connectionStringBuilder.ConnectionString);
_dbd = new dbMySQL(_mysqlconn, false);
}
else
{
var connectionStringBuilder = new SqlConnectionStringBuilder();
connectionStringBuilder.DataSource = connInfo.dbServer;
connectionStringBuilder.UserID = connInfo.dbUser;
connectionStringBuilder.Password = connInfo.dbPassword;
connectionStringBuilder.InitialCatalog = connInfo.dbName;
_dbd = new dbMSSQL(connectionStringBuilder.ConnectionString);
}
return _dbd;
}
使用上述所有内容,我可以成功访问MySQL或MSSQL中的数据:
_dbd = selectDbProvider(Id);
model = _dbd.ModelData.ToList();
但是,当我尝试进行更新操作时,我收到了顶部提到的错误消息。如何正确定义我的界面,使其包含DbContext类的Entry方法?
答案 0 :(得分:1)
为您的界面添加一个方法。
DbEntityEntry Entry(Object entity)
https://msdn.microsoft.com/en-us/library/gg696238(v=vs.113).aspx
编辑:
public class dbMyContext : DbContext
{
//snip
public dbMyContext(DbConnection existingConnection, boolcontextOwnsConnection) : base(existingConnection, contextOwnsConnection) { }
public dbMyContext(string connectionString) : base(connectionString) { }
//snip
}
调整selectDbProvider
类以使用dbMyContext而不是dbMySQL和dbMSSQL。
现在您正确使用O / RM。 :)