【发布时间】:2018-03-10 11:45:59
【问题描述】:
我对使用 Linq 和 Entity Framework 还很陌生,但我现在正尝试将行插入 SQL Server 数据库(更新行工作正常),但我无法找到以下错误的根本原因:
CS1503 参数 1:无法从“PurdisHeatingSolutions.NewCustomer”转换为“PurdisHeatingSolutions.Customer”
我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace PurdisHeatingSolutions
{
public partial class NewCustomer : System.Web.UI.Page
{
protected void Back_Click(object sender, EventArgs e)
{
Response.Redirect("~/Customers");
}
protected void CreateCustomerData_Click(object sender, EventArgs e)
{
using (var context = new PurdisHeatingSolutions.PHSContext())
{
Guid guid = new Guid();
NewCustomer cust = new NewCustomer
{
CustomerId = guid,
CustomerName = txtCustomerName.Text.Trim(),
CustomerAddress = txtCustomerAddress.Text.Trim(),
ContactNumber = txtCustomerNumber.Text.Trim(),
ContactEmail = txtCustomerEmail.Text.Trim()
};
txtCustomerAddress.Text = cust.CustomerName;
context.Customers.Add(cust);
}
}
}
public partial class NewCustomer
{
public NewCustomer() { }
public NewCustomer(Guid customerId, string customerName, string customerAddress, string customerContact, string customerEmail)
{
CustomerId = customerId;
CustomerName = customerName;
CustomerAddress = customerAddress;
ContactNumber = customerContact;
ContactEmail = customerEmail;
}
public Guid CustomerId { get; set; }
public string CustomerName { get; set; }
public string CustomerAddress { get; set; }
public string ContactNumber { get; set; }
public string ContactEmail { get; set; }
}
}
线上出现错误
context.Customers.Add(cust);
带有反对对象 cust 的论据。
我已检查对象项直接匹配上下文 Customers,但无法弄清楚我收到此问题的原因。
有人知道为什么吗?
【问题讨论】:
-
错误信息告诉你。
CustomersDbSet 适用于类型Customer而不是NewCustomer -
非常感谢,我似乎误解了这部分的工作原理,现在对我来说很有意义。
标签: c# asp.net linq entity-framework-6