简体   繁体   English

EntityFramework 7添加实体时出错

[英]EntityFramework 7 Error when adding entity

I'm getting the following error when trying to add a new entity to an Entity Framework data context. 尝试将新实体添加到Entity Framework数据上下文时出现以下错误。

An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code System.Data.dll中发生类型'System.Data.SqlClient.SqlException'的异常,但未在用户代码中处理

Additional information: Invalid object name 'DefaultSequence'. 附加信息:无效的对象名称'DefaultSequence'。

I have no idea where DefaultSequence is coming from. 我不知道DefaultSequence的来源。 When the add method is executed I can see it query SQL server something like select next result from DefaultSequence (I don't have that in front of me right now, but something like that was the query that was executed. 当执行add方法时,我可以看到它查询SQL Server,例如从DefaultSequence中选择下一个结果(我现在还没有那个,但是类似的是执行的查询。

Here is the code that I'm using to add the entity and the error is happening on the .Add line: 这是我用来添加实体的代码,该错误发生在.Add行上:

public IActivityCode Create(ActivityCode item)
    {
        data.ActivityCode.Add(item);
        data.SaveChanges();

        return Get(item.Id);
    }

Here is the code for the DbContext 这是DbContext的代码

public class MyDbContext : DbContext
{

    public DbSet<ActivityCode> ActivityCode { get; set; }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer(Startup.Configuration.Get("Data:DefaultConnection:ConnectionString"));
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<ActivityCode>().ForSqlServer((builder => builder.Table("ActivityCode", "Payments")));

        base.OnModelCreating(modelBuilder);
    }
}

I needed to do the below to get this to work. 我需要执行以下操作才能使其正常工作。 The comment Jurgen was close, but didn't work as posted, but if you want to post as answer I will accept that as an answer as well. 于尔根(Jurgen)的评论很接近,但不能按已发布的方式工作,但是如果您要发布作为答案,我也将其作为答案。

    modelBuilder.Entity<ActivityCode>().ForSqlServer(builder => builder.Table("ActivityCode", "Payments"));
    modelBuilder.Entity<EquityActivity>().ForSqlServer(builder => builder.Table("EquityActivity", "Equity"));
    modelBuilder.ForSqlServer().UseIdentity();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM