简体   繁体   English

更新操作显示实体框架中的错误

[英]update operation showing error in Entity framework

I am trying to update two table using entity framework but its showing error :"The entity type <>f__AnonymousType3`4 is not part of the model for the current context". 我正在尝试使用实体框架更新两个表,但是显示错误:“实体类型<> f__AnonymousType3`4不是当前上下文模型的一部分”。

protected void Button3_Click(object sender, EventArgs e)
    {
        var obj = Convert.ToInt32(TextBox5.Text);
        var v = (from m in de.t1
                 join n in de.t2
                 on m.Id equals n.id
                 where m.Id == obj
                 select new { m.Name,m.Age,m.Address,n.salary}).FirstOrDefault();
        if (v != null)
        {
            t1 t = new t1
            {
                Name = TextBox1.Text,
                Age = Convert.ToInt32(TextBox2.Text),
                Address = TextBox3.Text,
                t2 = new t2
                {
                    salary = Convert.ToInt64(TextBox4.Text)
                }
            };
        de.Entry(v).State = System.Data.Entity.EntityState.Modified;
        de.SaveChanges();
        }

    }

v is not one of your entities. v不是您的实体之一。 Did you mean de.Entry(t).State ? 您是说de.Entry(t).State吗?

You can avoid this sort of error in the future by taking advantage of strongly-typed collections instead of invoking db.Entry() for everything: 您可以通过使用强类型集合而不是为所有内容调用db.Entry()来避免将来发生此类错误:

    if (v != null)
    {
        t1 t = new t1
        {
            Name = TextBox1.Text,
            Age = Convert.ToInt32(TextBox2.Text),
            Address = TextBox3.Text,
            t2 = new t2
            {
                salary = Convert.ToInt64(TextBox4.Text)
            }
        };
        // de.Entry(t).State = System.Data.Entity.EntityState.Added;
        de.t1.Add(t);
        de.SaveChanges();
    }

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

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