gpt4 book ai didi

c# - 使用 Entity Framework 将新记录插入数据库

转载 作者:太空宇宙 更新时间:2023-11-03 20:24:59 25 4
gpt4 key购买 nike

我的数据库中有这样一个表:

[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,

id 被定义为主键。

我想在这个表中插入一条新记录。这是我无法开始工作的代码。

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");
}

我得到的错误是在线 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'

我需要更改什么才能使其正常工作?

最佳答案

您需要向方法 AddToCustomer_Downloads 提供类型为 CustomerPortalMVC.Models.Customer_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");
}

关于c# - 使用 Entity Framework 将新记录插入数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11109272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com