我在尝试在DB上创建记录时遇到了一些问题。我正在使用Entity Framework和SQL Server。
我做了一些研究,但找不到任何帮助我的东西。我看到很多人提到.edmx
文件上的主键或映射。我根据我找到的答案更改了我的.edmx
文件,尝试删除一些密钥,只留下一个以及更改存储:键入表而不是视图。主键显示在.edmx
上,但我没有设置它,因为我认为它是自动生成的。
部分.edmx
<!--Errors Found During Generation:
warning 6002: The table/view 'xxx.dbo.FilteredNew_timesheetlineitem' does not have a primary key defined. The key has been inferred and the definition was created as a read-only table/view.-->
<EntityType Name="FilteredNew_timesheetlineitem">
<Key>
<PropertyRef Name="createdbydsc" />
<PropertyRef Name="modifiedbydsc" />
<PropertyRef Name="new_accounttimesheetlineitemiddsc" />
<PropertyRef Name="new_approvedbyuseriddsc" />
<PropertyRef Name="new_billratetimesheetlineitemiddsc" />
<PropertyRef Name="new_billtoiddsc" />
<PropertyRef Name="new_candidatetimesheetlineitemiddsc" />
<PropertyRef Name="new_contracttypetimesheetlineitemiddsc" />
<PropertyRef Name="new_paytypetimesheetlineitemiddsc" />
<PropertyRef Name="new_slatimesheetlineitemiddsc" />
<PropertyRef Name="new_stream3timesheetlineitemiddsc" />
<PropertyRef Name="new_timesheetlineitemid" />
<PropertyRef Name="new_timesheettimesheetlineitemiddsc" />
<PropertyRef Name="ownerid" />
<PropertyRef Name="owneriddsc" />
<PropertyRef Name="statecode" />
<PropertyRef Name="transactioncurrencyiddsc" />
</Key>
<Property Name="createdby" Type="uniqueidentifier" />
<Property Name="createdbydsc" Type="int" Nullable="false" />
<Property Name="createdbyname" Type="nvarchar" MaxLength="160" />
<Property Name="createdbyyominame" Type="nvarchar" MaxLength="160" />
<Property Name="createdon" Type="datetime" />
<Property Name="createdonutc" Type="datetime" />
<Property Name="createdonbehalfby" Type="uniqueidentifier" />
<Property Name="createdonbehalfbyname" Type="nvarchar" MaxLength="160" />
<Property Name="createdonbehalfbyyominame" Type="nvarchar" MaxLength="160" />
<Property Name="exchangerate" Type="decimal" Precision="23" Scale="10" />
<Property Name="importsequencenumber" Type="int" />
<Property Name="modifiedby" Type="uniqueidentifier" />
<Property Name="modifiedbydsc" Type="int" Nullable="false" />
<Property Name="modifiedbyname" Type="nvarchar" MaxLength="160" />
<Property Name="modifiedbyyominame" Type="nvarchar" MaxLength="160" />
<Property Name="modifiedon" Type="datetime" />
<Property Name="modifiedonutc" Type="datetime" />
<Property Name="modifiedonbehalfby" Type="uniqueidentifier" />
<Property Name="modifiedonbehalfbyname" Type="nvarchar" MaxLength="160" />
<Property Name="modifiedonbehalfbyyominame" Type="nvarchar" MaxLength="160" />
<Property Name="new_accounttimesheetlineitemid" Type="uniqueidentifier" />
<Property Name="new_accounttimesheetlineitemiddsc" Type="int" Nullable="false" />
<Property Name="new_accounttimesheetlineitemidname" Type="nvarchar" MaxLength="160" />
<Property Name="new_accounttimesheetlineitemidyominame" Type="nvarchar" MaxLength="160" />
<Property Name="new_approved" Type="bit" />
<Property Name="new_approvedbyuserid" Type="uniqueidentifier" />
<Property Name="new_approvedbyuseriddsc" Type="int" Nullable="false" />
<Property Name="new_approvedbyuseridname" Type="nvarchar" MaxLength="160" />
<Property Name="new_approvedbyuseridyominame" Type="nvarchar" MaxLength="160" />
<Property Name="new_approveddatetime" Type="datetime" />
<Property Name="new_approveddatetimeutc" Type="datetime" />
<Property Name="new_approvedname" Type="nvarchar" MaxLength="4000" />
<Property Name="new_billrate" Type="money" />
<Property Name="new_billratetimesheetlineitemid" Type="uniqueidentifier" />
<Property Name="new_billratetimesheetlineitemiddsc" Type="int" Nullable="false" />
<Property Name="new_billratetimesheetlineitemidname" Type="nvarchar" MaxLength="100" />
<Property Name="new_billrate_base" Type="money" />
<Property Name="new_billtoid" Type="uniqueidentifier" />
<Property Name="new_billtoiddsc" Type="int" Nullable="false" />
<Property Name="new_billtoidname" Type="nvarchar" MaxLength="160" />
<Property Name="new_billtoidyominame" Type="nvarchar" MaxLength="160" />
<Property Name="new_candidatetimesheetlineitemid" Type="uniqueidentifier" />
<Property Name="new_candidatetimesheetlineitemiddsc" Type="int" Nullable="false" />
<Property Name="new_candidatetimesheetlineitemidname" Type="nvarchar" MaxLength="100" />
<Property Name="new_contracttypetimesheetlineitemid" Type="uniqueidentifier" />
<Property Name="new_contracttypetimesheetlineitemiddsc" Type="int" Nullable="false" />
<Property Name="new_contracttypetimesheetlineitemidname" Type="nvarchar" MaxLength="100" />
<Property Name="new_enddatetime" Type="datetime" />
<Property Name="new_enddatetimeutc" Type="datetime" />
<Property Name="new_firstname" Type="nvarchar" MaxLength="100" />
<Property Name="new_hours" Type="decimal" Precision="23" Scale="10" />
<Property Name="new_jobseekerid" Type="nvarchar" MaxLength="100" />
<Property Name="new_lastname" Type="nvarchar" MaxLength="100" />
<Property Name="new_lunchtime" Type="int" />
<Property Name="new_lunchtimename" Type="nvarchar" MaxLength="4000" />
<Property Name="new_name" Type="nvarchar" MaxLength="100" />
<Property Name="new_payrollreference" Type="nvarchar" MaxLength="100" />
<Property Name="new_paytypetimesheetlineitemid" Type="uniqueidentifier" />
<Property Name="new_paytypetimesheetlineitemiddsc" Type="int" Nullable="false" />
<Property Name="new_paytypetimesheetlineitemidname" Type="nvarchar" MaxLength="100" />
<Property Name="new_slatimesheetlineitemid" Type="uniqueidentifier" />
<Property Name="new_slatimesheetlineitemiddsc" Type="int" Nullable="false" />
<Property Name="new_slatimesheetlineitemidname" Type="nvarchar" MaxLength="100" />
<Property Name="new_startdatetime" Type="datetime" />
<Property Name="new_startdatetimeutc" Type="datetime" />...
以下保存ViewModel数据的方法
public void SaveTimesheetLine(TimesheetViewModel timesheetLineVm)
{
string payrollId = Convert.ToString(Session["payroll"]);
using (MSCRMEntities t = new MSCRMEntities())
{
var line = new FilteredNew_timesheetlineitem
{
new_billtoid = timesheetLineVm.TimesheetLineViewModels.BillToId,
new_timesheettimesheetlineitemid = timesheetLineVm.TimesheetId,
new_slatimesheetlineitemid = timesheetLineVm.TimesheetLineViewModels.SlaId,
new_billratetimesheetlineitemid = timesheetLineVm.TimesheetLineViewModels.BillRateId,
new_stream3timesheetlineitemid = timesheetLineVm.TimesheetLineViewModels.Stream3Id,
new_contracttypetimesheetlineitemid = timesheetLineVm.TimesheetLineViewModels.ContractTypeId,
new_firstname = timesheetLineVm.TimesheetLineViewModels.EmployeeFirstName,
new_lastname = timesheetLineVm.TimesheetLineViewModels.EmployeeLastName,
new_accounttimesheetlineitemid = timesheetLineVm.TimesheetLineViewModels.EmployerId,
new_payrollreference = payrollId,
new_timesheetdate = timesheetLineVm.TimesheetDate,
new_candidatetimesheetlineitemid = timesheetLineVm.TimesheetLineViewModels.CandidateId,
// taken from user input
new_startdatetime = timesheetLineVm.TimesheetLineViewModels.StartDate,
new_enddatetime = timesheetLineVm.TimesheetLineViewModels.EndDate,
new_paytypetimesheetlineitemidname = Convert.ToString(timesheetLineVm.TimesheetLineViewModels.PayType),
new_lunchtime = 2,
new_submittedhours = timesheetLineVm.TimesheetLineViewModels.SubmittedHours
};
t.FilteredNew_timesheetlineitem.Add(line);
// t.Entry(line).State = (EntityState) Microsoft.Xrm.Sdk.EntityState.Created;
t.SaveChanges();
}
}
我得到的错误是:
无法更新EntitySet,因为它有一个defineQuery而且没有 InsertFnction元素存在于modificationFunctionMapping中 元素支持当前操作。
有谁知道为什么会出现这样的错误?
提前致谢, DEM
答案 0 :(得分:1)
显然,&#39; xxx.dbo.FilteredNew_timesheetlineitem&#39;没有主键。
答案 1 :(得分:1)
Dynamics CRM DB无法直接更新,您需要使用CRM Web服务而不是SQL查询或EntityFramework。