实体框架不能单增加 [英] Entity framework can't add with singleton

查看:116
本文介绍了实体框架不能单增加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我个人类:

 public class User
{
    public int Id { get; private set; }

    public string Isim { get; set; }

    public string Soyad { get; set; }

    public User(string isim, string soyad)
    {
        Isim = isim;
        Soyad = soyad;
    }
}

我UserBusiness类:

My UserBusiness class:

public sealed class UserBusiness
{
    JuqueryDbEntities entity = new JuqueryDbEntities();

    private static volatile UserBusiness instance;
    private static readonly object syncRoot = new Object();

    private UserBusiness() { }

    public static UserBusiness Instance
    {
        get
        {
            if (instance == null)
            {
                lock (syncRoot)
                {
                    if (instance == null)
                        instance = new UserBusiness();
                }
            }

            return instance;
        }
    }

    public void AddUser(User userToAdd)
    {
        entity.PersonelTable.Add(userToAdd);
        entity.SaveChanges();
    }
}

和最后我的网页表单codebehind的onclick的登录按钮:

And lastly my webform codebehind onclick of a login button:

protected void Button1_Click(object sender, EventArgs e)
    {
        string isim = TextBox1.Text;
        string soyad = TextBox2.Text;
        var newUser = new User(isim, soyad);
        UserBusiness.Instance.AddUser(newUser);  
    }

下面是我的问题:我得到了我的UserBusiness类ADDUSER方法的错误。
在一个错误entity.PersonelTable.Add(NEWUSER);'行说
System.Data.Entity.DbSet.Add(SuperQquery.PersonelTable)'为最佳重载方法匹配''有一些无效的参数。我在做什么错了(顺便说一下我的ID是自动增量,所以我没有设置任何值。)

Here is my problem: I get an error in AddUser method of my UserBusiness class. An error in 'entity.PersonelTable.Add(newUser);' line says 'The best overload method match for 'System.Data.Entity.DbSet.Add(SuperQquery.PersonelTable)' has some invalid arguments.' What am I doing wrong(Btw my Id is autoincrement so I am not setting it any value.)

推荐答案

在这里你的问题是,该方法添加 System.Data.Entity的的.DbSet 也根本用不了类型的用户的参数。
就像提到的错误

your problem here is that the method Add of System.Data.Entity.DbSet does simply not take argument of type user. like mentioned in the error

System.Data.Entity.DbSet.Add(SuperQquery.PersonelTable)'具有一些
  无效的参数

System.Data.Entity.DbSet.Add(SuperQquery.PersonelTable)' has some invalid arguments

它需要类型的对象 SuperQquery.PersonelTable

所以,你需要做的是改变ADDUSER方法

So what you need to do is change AddUser method

public void AddUser(User userToAdd)
{
   SuperQquery.PersonelTable pt = new SuperQquery.PersonelTable();
   pt.FieldName1 = userToAdd.Isim;
   pt.FieldName2 = userToAdd.Soyad;

   entity.PersonelTable.Add(pt);
   entity.SaveChanges();
}

,其中字段名是数据库中的表中的列名

where FieldName are names of columns in the table of the database

编辑:如果这个工程,我们可以说,你做类型的用户的转换输入SuperQquery.PersonelTable您可以将数据库中的数据的方式,但在这里,最好的方式是通过一个新的类PersonelTable更改类用户具有相同的逻辑。没有必要做转换。

if this works, we can say that you made a conversion of type user to type SuperQquery.PersonelTable the way you can insert data in database, but the best way here is to change the class User by a new class PersonelTable with the same logic. No need to do a conversion.

这篇关于实体框架不能单增加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆