如何在C#和实体框架中使用自动增量将一些值插入到表中?

如何在C#和实体框架中使用自动增量将一些值插入到表中?

问题描述:

我不能(通过)NAME,ABBR到我的insert_city函数

i cant give(pass) NAME,ABBR to my insert_city function

public string city, abbr;

public string NAME
{
get { return city; }
set { city = value; }
}
public string ABBR
{
get { return abbr; }
set { abbr = value; }
}










public static string insert_city()
{

SqlParameter[] p = new SqlParameter[2];
p[0] = new SqlParameter("@NAME",NAME.get);
p[1] = new SqlParameter("@ABBR",ABBR.get);
return DAL.project.SqlDataProvider.insert_city(p);
}





我的尝试:



使用3个架构和两个类库(bll,dal)

我的dals代码是:





公共静态字符串insert_city()

{

bookingEntities db = new bookingEntities();

CITY ct = new CITY();

ct.NAME = @NAME;

ct.ABBR = @ABBR;

db.CITies.Add(ct);

db.SaveChanges();

返回ct.NAME;

}







我的BELL代码:



使用System;

使用System。 Collections.Generic;

使用System.Linq;

使用System.Text;

使用System.Threading.Tasks;

使用DAL.project;



使用System.Data;

使用System.Configuration;

使用System.Linq;

使用System.Web;





使用System.Xml.Linq;

使用System.Data.SqlClient;

命名空间BLL.project

{

公共类DataProvider

{

public DataProvider()

{



}

公共字符串城市,abbr;



公共字符串NAME

{

get {return city; }

set {city = value; }

}

公共字符串ABBR

{

get {return abbr; }

set {abbr = value; }

}



//

公共静态字符串insert_city()

{$ / $


SqlParameter [] p = new SqlParameter [2];

p [0] =新的SqlParameter(@ NAME,NAME。 get);

p [1] =新的SqlParameter(@ ABBR,ABBR.get);

返回DAL.project.SqlDataProvider.insert_city(p);

}



和我的persentaions代码:



公共部分类Default4: System.Web.UI.Page

{

DataProvider obj = new DataProvider();

protected void Button15_Click(object sender,EventArgs e)

{

obj.city = Convert.ToString(TextBox4.Text);

obj.abbr = Convert.ToString(TextBox5.Text) ;

if(!String.IsNullOrEmpty(obj.city))

{

if(!String.IsNullOrEmpty(obj.abbr))
{

string ci = BLL.project.DataProvider.insert_city();

Label28.Text =شهر+ ci +بهلیست شهرهااضافهشد。;

}

else

{

Label28.Text =اختصارنمیتواندخالی باشد。;

}

}

else

{

Label28。 Text =نامشهرنمیتواندخالیباشد。;

}

}

}



What I have tried:

is used 3tire architecture with two class library(bll,dal)
my dals code is :


public static string insert_city()
{
bookingEntities db = new bookingEntities();
CITY ct = new CITY();
ct.NAME = @NAME;
ct.ABBR = @ABBR;
db.CITies.Add(ct);
db.SaveChanges();
return ct.NAME;
}



in my blls code :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DAL.project;

using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;


using System.Xml.Linq;
using System.Data.SqlClient;
namespace BLL.project
{
public class DataProvider
{
public DataProvider()
{

}
public string city, abbr;

public string NAME
{
get { return city; }
set { city = value; }
}
public string ABBR
{
get { return abbr; }
set { abbr = value; }
}

//
public static string insert_city()
{

SqlParameter[] p = new SqlParameter[2];
p[0] = new SqlParameter("@NAME",NAME.get);
p[1] = new SqlParameter("@ABBR",ABBR.get);
return DAL.project.SqlDataProvider.insert_city(p);
}

and in my persentaions code :

public partial class Default4 : System.Web.UI.Page
{
DataProvider obj = new DataProvider();
protected void Button15_Click(object sender, EventArgs e)
{
obj.city = Convert.ToString(TextBox4.Text);
obj.abbr = Convert.ToString(TextBox5.Text);
if (!String.IsNullOrEmpty(obj.city))
{
if (!String.IsNullOrEmpty(obj.abbr))
{
string ci = BLL.project.DataProvider.insert_city();
Label28.Text = "شهر " + ci + " به لیست شهر ها اضافه شد.";
}
else
{
Label28.Text = "اختصار نمیتواند خالی باشد.";
}
}
else
{
Label28.Text = " نام شهر نمی تواند خالی باشد.";
}
}
}

没有看到你的 CITY 类的声明,很难说。但听起来您忘记将 DatabaseGenerated 属性添加到 CITYID 属性:

Without seeing the declaration of your CITY class, it's hard to tell. But it sounds like you've forgotten to add the DatabaseGenerated attribute to the CITYID property:
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public virtual int CITYID { get; set; }


发生错误是因为您尝试将值设置为自动增量字段CITYID。



无法手动设置自动增量字段,因为数据库服务器本身会通过增加上次使用的值来自动生成其值。



所以你必须像这样纠正你的代码:



The error occurred because you are trying to set a value into your your auto-increment field CITYID.

The auto-increment field cannot be set manually, because its values will be automatically generated by incremented the last used value, by the database server itself.

So you have to correct your code like this:

bookingEntities db = new bookingEntities(); 
CITY ct = new CITY();
ct.NAME = city_name;
ct.ABBR = city_abbr;
db.CITies.Add(ct);
db.SaveChanges();





现在您已经提供了更多详细信息,它表示您正在使用代码第一种方法,所以解决方案是使用自动增量字段的下一个属性:



Now that you gave us more details, it seams that you are using code first approach, so the solution is to use the next attribute for your Auto-Increment fields:

[DatabaseGenerated(DatabaseGeneratedOption.Identity)]

您可以在下一篇文章中找到更多详细信息: 使用实体框架中的属性构建代码优先模型 [ ^ ]

You can find more details in the next article: Building a Code-First Model Using Attributes in Entity Framework[^]


查看此 c# - 使用实体框架的自动编号 - 堆栈溢出 [ ^ ]