C# 使用实体框架将新记录插入数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11109272/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Insert new record into database using Entity Framework
提问by Mr Lahey
I have a table in my database like this:
我的数据库中有一个表,如下所示:
[id] [uniqueidentifier] NOT NULL,
[user_id] [uniqueidentifier] NOT NULL,
[download_type] [nvarchar](50) NOT NULL,
[download_id] [nvarchar](50) NOT NULL,
[download_date] [datetime] NOT NULL,
[user_ip_address] [nvarchar](20) NOT NULL,
The idis defined to be the primary key.
在id被定义为的主键。
I want to insert a new record into this table. Here is my code I cannot get to work.
我想在这个表中插入一条新记录。这是我无法开始工作的代码。
CustomerPortalEntities_Customer_Downloads dbcd = new CustomerPortalEntities_Customer_Downloads();
public ActionResult Download(string id)
{
var collection = new FormCollection();
collection.Add("user_id", Membership.GetUser().ProviderUserKey.ToString());
collection.Add("download_type", "Car");
collection.Add("download_id", id);
collection.Add("download_date", DateTime.Now.ToString());
collection.Add("user_ip_address", Request.ServerVariables["REMOTE_ADDR"]);
dbcd.AddToCustomer_Downloads(collection);
return Redirect("../../Content/files/" + id + ".EXE");
}
The errors I get is on the line dbcd.AddToCustomer_Downloads(collection);
我得到的错误是 dbcd.AddToCustomer_Downloads(collection);
The best overloaded method match for 'CustomerPortalMVC.Models.CustomerPortalEntities_Customer_Downloads.AddToCustomer_Downloads(CustomerPortalMVC.Models.Customer_Downloads)' has some invalid arguments
Argument '1': cannot convert from 'System.Web.Mvc.FormCollection' to 'CustomerPortalMVC.Models.Customer_Downloads'
“CustomerPortalMVC.Models.CustomerPortalEntities_Customer_Downloads.AddToCustomer_Downloads(CustomerPortalMVC.Models.Customer_Downloads)”的最佳重载方法匹配有一些无效参数
参数“1”:无法从“System.Web.Mvc.FormCollection”转换为“CustomerPortalMVC.Models.Customer_Downloads”
What do I need to change to make this work?
我需要改变什么才能使这项工作?
采纳答案by RePierre
You need to provide an object of type CustomerPortalMVC.Models.Customer_Downloadsto method AddToCustomer_Downloadsand then call SaveChangeson your data context like this:
您需要为CustomerPortalMVC.Models.Customer_Downloadsmethod提供一个类型的对象,AddToCustomer_Downloads然后SaveChanges像这样调用您的数据上下文:
public ActionResult Download(string id)
{
var item = new CustomerPortalMVC.Models.Customer_Downloads();
item.user_id = Membership.GetUser().ProviderUserKey.ToString();
item.download_type = "Car";
item.download_id = id;
item.download_date = DateTime.Now.ToString();
item.user_ip_address = Request.ServerVariables["REMOTE_ADDR"];
dbcd.AddToCustomer_Downloads(item);
dbcd.SaveChanges();
return Redirect("../../Content/files/" + id + ".EXE");
}
回答by Sven Grosen
You need to create an instance of your Customer_Downloads class and pass that to your AddToCustomer_Downloads method. The error message is telling you that the interface for that method expects a Customer_Downloads object but you are passing it a FormCollection object.
您需要创建 Customer_Downloads 类的实例并将其传递给您的 AddToCustomer_Downloads 方法。错误消息告诉您该方法的接口需要一个 Customer_Downloads 对象,但您将它传递给一个 FormCollection 对象。

