使用n层arch中的实体框架在数据库中插入值的问题 [英] Problen in insert value in database using entity framework in n-tier arch

查看:60
本文介绍了使用n层arch中的实体框架在数据库中插入值的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表ID,状态,反馈中有3列.Id是身份,状态值bydefault是false,我必须在n层arch中使用实体框架插入反馈值。



我做了3个课程



BO

I have 3 column in my table ID,Status,feedback.Id is identity,status value bydefault is false and I have to insert value of feedback using entity framework in n-tier arch.

I made 3 classes as

BO

namespace problemvisa
{
    public class Bo
    {
        private int id;
 
        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        private string status;
 
        public string Status
        {
            get { return status; }
            set { status = value; }
        }
        private string feedback;
 
        public string Feedback
        {
            get { return feedback; }
            set { feedback = value; }
        }
    }
}



2-DAL


2-DAL

public class DAL
{
    public bool insert(Bo aa)
    {
        if (aa.Equals(null))
        {
            return false;
        }
        else
        {
            using (problemEntities1 context = new problemEntities1())
            {
                Table1 tt = new Table1();
                 context.AddToTable1(tt);
                 context.SaveChanges();
            }
            return true;
        }
    }
}



3.BAL


3.BAL

public class BAL
{
    public bool insert(Bo ss)
    {
        DAL dd = new DAL();
        if (dd.insert(ss))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}



IN aspx表单


IN aspx form

protected void Button1_Click(object sender, EventArgs e)
{    
    Bo aa = new Bo();
    aa.Status = "false";
    aa.Feedback = TextBox1.Text;
    BAL ss = new BAL();
    ss.insert(aa);
}







一个错误即将来临ieCannot将值NULL插入列''反馈'',表''problem.dbo.Table1'';列不允许空值。 INSERT失败。

语句已终止。




One error is coming i.e.Cannot insert the value NULL into column ''feedback'', table ''problem.dbo.Table1''; column does not allow nulls. INSERT fails.
The statement has been terminated.

推荐答案

确定首先检查数据库ID字段是否为主键,如果你没有设置主键然后设置它并检查。
Ok First check your database that the ID field is Primary Key or not , if you have not set primary key then set it and check.


因为,你的数据库表不接受反馈列的空值。请仔细检查此代码:

Because, your database table doesn''t accepts null value for feedback column. Check this code very carefully:
public class DAL
{
    public bool insert(Bo aa)
    {
        if (aa.Equals(null))
        {
            return false;
        }
        else
        {
            using (problemEntities1 context = new problemEntities1())
            {
                 Table1 tt = new Table1();
                 context.AddToTable1(tt);
                 context.SaveChanges();
            }
            return true;
        }
    }
}



如果对象aa不为null,则调试器将移至else块。您没有在表中插入aa的值。您只是创建Table1的对象(它将所有列值都设置为null)并直接将其注入到上下文中。试试这个:


If object aa is not null then the debugger will move to else block. You are not inserting the value of aa in your table. You are just creating the object of your Table1(it''ll have all the column values as null) and directly instering that onject to the context. Try this:

Table1 tt = new Table1();
tt.Status = aa.Status;
tt.Feedback = aa.Feedback;//status and feedback is your database columns.
context.AddToTable1(tt);
context.SaveChanges();







--Amit




--Amit


代替TextBox1.Text放置一个值并检查它是否正常工作。
in the place of TextBox1.Text put one value and check whether it is working or not.


这篇关于使用n层arch中的实体框架在数据库中插入值的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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