【问题标题】:Raw SQL command not executing using Entity Framework未使用实体框架执行的原始 SQL 命令
【发布时间】:2014-10-27 06:17:01
【问题描述】:

我在使用以下原始 SQL 命令和实体框架播种数据时遇到问题

databaseContext.Database.ExecuteSqlCommand
      (@"INSERT INTO    NatureOfBusinessRiskClass
            ( ProductSectionId ,
            NatureOfBusinessId ,
            RiskClassId ,
            ConstructionType ,
            AreaType ,
            SumInsuredLimit ,
            MaximumRate ,
            MinimumRate ,
            IsAggregated ,
            CreatedBy ,
            CreateDate)
      SELECT P.Id,T.NatureOdBusinessId,T.RiskClassId,ConstructionType,AreaType,0.00,
      0,0,0,1,GETDATE() FROM TempNatureBusiness T
            CROSS JOIN TempAreaType A
            CROSS JOIN TempConstructionType C
            CROSS JOIN ProductSection P");

当我在 Microsoft SQL Server Management Studion 中执行此 SQL 语句时,一切都按预期工作,但是当我使用实体框架时,表没有填充,也没有生成异常。

【问题讨论】:

  • 使用 SQL Profiler 检查是否使用了相同的查询,如果它与其他 sql 用户一起执行,请确保已授予权限
  • @user65439 试试这个:Var rowsaffected=databaseContext.Database.ExecuteSqlCommand(....);

标签: c# sql entity-framework rawsql


【解决方案1】:

问题是我还使用 RAW sql 命令来填充此查询所依赖的表以生成其内容,例如

databaseContext.Database.ExecuteSqlCommand("INSERT INTO TempAreType...");
databaseContext.Database.ExecuteSqlCommand("INSERT INTO TempConstructionType...");

然后我执行了我遇到问题的命令,

databaseContext.Database.ExecuteSqlCommand
  (@"INSERT INTO    NatureOfBusinessRiskClass
        ( ProductSectionId ,
        NatureOfBusinessId ,
        RiskClassId ,
        ConstructionType ,
        AreaType ,
        SumInsuredLimit ,
        MaximumRate ,
        MinimumRate ,
        IsAggregated ,
        CreatedBy ,
        CreateDate)
  SELECT P.Id,T.NatureOdBusinessId,T.RiskClassId,ConstructionType,AreaType,0.00,
  0,0,0,1,GETDATE() FROM TempNatureBusiness T
        CROSS JOIN TempAreaType A
        CROSS JOIN TempConstructionType C
        CROSS JOIN ProductSection P");

最后我执行了

databaseContext.SaveChanges();

显然这个语句会提交我的数据库更改,所以TempAreaTypeTempConstructionType 表在执行第三个命令时没有任何数据。当我在 Microsoft SQL Server Management Studio 中执行命令时,我的更改已经提交到数据库并且命令有效。

解决方案是调用

databaseContext.SaveChanges();

在第一个 to 命令之后,然后在最后,例如

databaseContext.Database.ExecuteSqlCommand("INSERT INTO TempAreType...");
databaseContext.Database.ExecuteSqlCommand("INSERT INTO TempConstructionType...");

databaseContext.SaveChanges();

databaseContext.Database.ExecuteSqlCommand
  (@"INSERT INTO    NatureOfBusinessRiskClass
        ( ProductSectionId ,
        NatureOfBusinessId ,
        RiskClassId ,
        ConstructionType ,
        AreaType ,
        SumInsuredLimit ,
        MaximumRate ,
        MinimumRate ,
        IsAggregated ,
        CreatedBy ,
        CreateDate)
  SELECT P.Id,T.NatureOdBusinessId,T.RiskClassId,ConstructionType,AreaType,0.00,
  0,0,0,1,GETDATE() FROM TempNatureBusiness T
        CROSS JOIN TempAreaType A
        CROSS JOIN TempConstructionType C
        CROSS JOIN ProductSection P");

databaseContext.SaveChanges();

【讨论】:

    猜你喜欢
    • 2011-02-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-28
    • 2015-06-16
    • 1970-01-01
    • 2017-06-12
    相关资源
    最近更新 更多