将表添加到Entity Framework后,从SQL查询到现有表的对象名称错误无效

时间:2021-09-15 02:14:40

I am Currently updating legacy ASP.Net Code to MVC5. There is already a database connected to the application. There was a requirement to add new tables to the application so I set up Entity Framework CF.

我目前正在将旧版ASP.Net代码更新为MVC5。已经有一个数据库连接到该应用程序。需要向应用程序添加新表,因此我设置了实体框架CF.

Now I need to get data from an existing table so I created the Model and used Fluid API to configure the table. The existing table had no Primary Key so I created a composite key. Finally I added the Configuration to the data context.

现在我需要从现有表中获取数据,因此我创建了Model并使用Fluid API来配置表。现有表没有主键,因此我创建了一个复合键。最后,我将配置添加到数据上下文中。

This broke some existing code in the Global.asax file of the old asp.net code. The code was a SQL statement to get some values from the same table.

这打破了旧asp.net代码的Global.asax文件中的一些现有代码。代码是一个SQL语句,用于从同一个表中获取一些值。

Here is my code

这是我的代码

Data Context

public class MyDBContext : DbContext
{
    public DbSet<EtlParameter> EtlParameter { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        // add models to Entity Framework
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
        .....
        modelBuilder.Configurations.Add(new EtlParameterConfiguration<EtlParameter>());
        base.OnModelCreating(modelBuilder);
    }
}

The Model

public class EtlParameter
{
    public string EtlName { get; set; }

    public int StepNumber { get; set; }

    public string ParameterName { get; set; }

    public string TextValue { get; set; }

    public int NumericValue { get; set; }

    public string RecordDescription { get; set; }
}

Fluent Configuration

public EtlParameterConfiguration()
{
    ToTable("ETL_PARAMETER", "dbo");

    HasKey(u => new { u.EtlName, u.StepNumber, u.ParameterName });

    Property(p => p.EtlName).HasColumnName("ETL_NM");
    Property(p => p.StepNumber).HasColumnName("STEP_NR");
    Property(p => p.ParameterName).HasColumnName("PARAMETER_NM");
    Property(p => p.TextValue).HasColumnName("TEXT_VL");
    Property(p => p.NumericValue).HasColumnName("NUMERIC_VL").IsOptional();
    Property(p => p.RecordDescription).HasColumnName("RECORD_DESC_TX").IsOptional();
}

This is the part of the Global file that is throwing the error

这是抛出错误的全局文件的一部分

protected void Session_Start(object sender, EventArgs e)
{
        ......
        populated from etl_paramter table.
        List<ConfigItem> cItems = new List<ConfigItem>();

        var connString = System.Configuration.ConfigurationManager.ConnectionStrings["MyDB_ConnectionString1"].ConnectionString;

        SqlConnection sqlConn = new SqlConnection(connString);

        //SqlConnection sqlConn = new SqlConnection(Properties.Settings.Default.MyDB_ConnectionString);
        SqlCommand cmd = new SqlCommand("Select role from operators_llk where operator_nm=@user_nm", sqlConn);
        SqlCommand cmd2 = new SqlCommand("select TOP 100 PARAMETER_NM,STEP_NR,TEXT_VL,NUMERIC_VL " +
        "from etl_parameter " +
        "where ETL_NM = 'mfgtransactions' " +
        "order by PARAMETER_NM,STEP_NR ", sqlConn);
        sqlConn.Open();
        if (!string.IsNullOrEmpty(Request.ServerVariables["HTTP_NTUSERDOMAINID"]))
        {
            var usr = Request.ServerVariables["HTTP_NTUSERDOMAINID"].Replace(":", "\\");
            cmd.Parameters.AddWithValue("@user_nm", usr);
            try
            {
                SqlDataReader reader = cmd.ExecuteReader();
                while (reader.Read())
                {
                    Session["UserRole"]= reader["role"].ToString().Trim();
                }
                reader.Close();
            }
            catch
            {
                sqlConn.Close();
                Session.Add("UserRole", usr + " " + Request.ServerVariables["HTTP_NTUSERDOMAINID"] + " " + cmd.CommandText);
            }
        }
        else
        {
        #if DEBUG
            Session.Add("UserRole", "ADMIN");
        #else
            Session.Add("UserRole", "USER");
        #endif
        }
        //get application settings
        try
        {
            SqlDataReader reader = cmd2.ExecuteReader();
            while (reader.Read())
            {
                cItems.Add(new ConfigItem(reader["PARAMETER_NM"].ToString(),
                    int.Parse(reader["STEP_NR"].ToString()),
                    reader["TEXT_VL"].ToString(),
                    reader["NUMERIC_VL"].ToString()));
            }
            reader.Close();
            Session["Configuration"] = cItems;
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            sqlConn.Close();
        }
    }

The Line that is giving the error is the last }. I have tried to remove any unnecessary lines as best as I could. I would like to know what I did that caused the conflict between ASP.Net and EF.

给出错误的Line是最后一个}。我试图尽可能地删除任何不必要的行。我想知道我做了什么导致ASP.Net和EF之间的冲突。

1 个解决方案

#1


0  

Everything in the code was fine. The issue was caused by a mistake in my migration that corrupted the database.

代码中的所有内容都很好。该问题是由于我的迁移错误导致数据库损坏。

When I first ran my migrations after creating the composite key I never used the -IgnoreChanges flag. When I realised this mistake I rolled back my migrations but I never checked what was in the Down migration.

当我在创建复合键后第一次运行迁移时,我从未使用-IgnoreChanges标志。当我意识到这个错误时,我回滚了我的迁移,但我从未检查过Down迁移中的内容。

After rebuilding the database and applying my changes with an empty migration every thing was fine.

重建数据库并使用空迁移应用我的更改后,每件事都很好。

#1


0  

Everything in the code was fine. The issue was caused by a mistake in my migration that corrupted the database.

代码中的所有内容都很好。该问题是由于我的迁移错误导致数据库损坏。

When I first ran my migrations after creating the composite key I never used the -IgnoreChanges flag. When I realised this mistake I rolled back my migrations but I never checked what was in the Down migration.

当我在创建复合键后第一次运行迁移时,我从未使用-IgnoreChanges标志。当我意识到这个错误时,我回滚了我的迁移,但我从未检查过Down迁移中的内容。

After rebuilding the database and applying my changes with an empty migration every thing was fine.

重建数据库并使用空迁移应用我的更改后,每件事都很好。