C# 将下拉列表值保存到SQL数据库条目

C# 将下拉列表值保存到SQL数据库条目,c#,asp.net,entity-framework,C#,Asp.net,Entity Framework,我正在为大学做一个练习,在那里我创建了一个SQL数据库,并在实体框架的帮助下将其链接到visualstudio中的网格视图。要在数据库中创建新条目,我必须填写一些文本字段(例如:名称、股票) 现在,我从一个文本字段中了解到,根据它是string、decimal或integer,我使用以下命令将每个文本字段绑定到数据库表中相应的部分,从而保存其值(.cs code): 如果我想在我的代码中添加一个下拉列表,并从中获取一个可用值并将其保存到给定表(类别)中正在创建的新条目中,我将如何完成以下代码位行

我正在为大学做一个练习,在那里我创建了一个
SQL数据库
,并在实体框架的帮助下将其链接到visualstudio中的网格视图。要在数据库中创建新条目,我必须填写一些文本字段(例如:名称、股票)

现在,我从一个文本字段中了解到,根据它是
string
decimal
integer
,我使用以下命令将每个文本字段绑定到数据库表中相应的部分,从而保存其值(.cs code):

如果我想在我的代码中添加一个
下拉列表
,并从中获取一个可用值并将其保存到给定表(类别)中正在创建的新条目中,我将如何完成以下代码位行:

entry.Category=???(bit that I don't know how to complete)

您的
Rugby
实体还应具有FK属性(
CategoryId
),这样您就可以保存下拉列表中的
SelectedValue
,以建立关系:

entry.CategoryId=dropdown.SelectedValue;
这是假设您以如下方式设置下拉列表:

var categories = (from c in context.Categories
                            select new { c.CategoryId, c.CategoryName }).ToList();

// Change this for the real property names
dropdown.DataValueField = "CategoryId";
dropdown.DataTextField = "CategoryName";
dropdown.DataSource = categories;
dropdown.DataBind(); 

如果您已经将EF图表引入Visual Studio(到您的.edmx文件中),并且我正确理解了您的表结构,那么您可以这样做:

 // This will be what you named your entities from your .edmx
 // This object sets up the connection to your entities 
 public static myEntities _db = new myEntities ();

(Auto-Generated Class via .edmx)
// When you add an .edmx file (database diagram for EF)
// There is an auto-generated class you are able to use - 
// We are setting this up below.
EF_Class_Name newDbEntry= new EF_Class_Name();


// You can access any property of the class just like any other object
// property.
newDbEntry.Stock = int.Parse(txtStock.Text);
newDbEntry.Name = txtName.Text;
newDbEntry.Category = dropdown.SelectedValue;


// Add your new data (essentially an insert)
_db.EF_Class_Name.Add(newDbEntry);

// Commit the changes
_db.SaveChanges();

您是否已经将EF图引入VS?是的,我有Model.edmx文件,我想这就是您的意思@confusedandmused这里没有SQL数据库-SQL只是结构化查询语言-许多数据库系统使用的一种语言,但它不是数据库产品。很多东西都是特定于供应商的,所以我们确实需要知道您使用的是什么数据库系统(以及哪个版本)。。。。(请相应地更新标记)@marc_s实体框架是否支持其他查询语言(除了MS flavor)?我不知道它是这样做的。@confusedandamused实体框架使用任何提供接口以插入数据后端的接口进行操作。(例如)很抱歉我的无知,但您将如何设置FK属性@octavioccl@JGuerra这是你在MSSQL服务器中设置的东西,然后更新你的图表来引入它。尽管我对评论中发生的事情感到非常困惑,但这是有效的。谢谢@JGuerra有什么我可以补充到我的答案中来帮助理解的吗?我现在要补充一点,也许可以澄清一些东西。谢谢你的更新,它确实有助于更好地理解它。对迟来的答复表示歉意。真是太棒了!
 // This will be what you named your entities from your .edmx
 // This object sets up the connection to your entities 
 public static myEntities _db = new myEntities ();

(Auto-Generated Class via .edmx)
// When you add an .edmx file (database diagram for EF)
// There is an auto-generated class you are able to use - 
// We are setting this up below.
EF_Class_Name newDbEntry= new EF_Class_Name();


// You can access any property of the class just like any other object
// property.
newDbEntry.Stock = int.Parse(txtStock.Text);
newDbEntry.Name = txtName.Text;
newDbEntry.Category = dropdown.SelectedValue;


// Add your new data (essentially an insert)
_db.EF_Class_Name.Add(newDbEntry);

// Commit the changes
_db.SaveChanges();