小编Hai*_*atz的帖子

EF忽略DatabaseGeneratedOption.None

我正在使用EF 6.1.3代码优先,但由于数据库已经存在,所以无法进行迁移。我有一个SRReports具有以下属性的实体:

[Key, Required]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.None)]
public int FRID { get; set; }
Run Code Online (Sandbox Code Playgroud)

DatabaseGeneratedOption.None假设FRID是自动生成的,实体框架将忽略,并将TSQL发送到服务器。为实体分配了FRID的值,但是实体框架忽略该值,并假定该值是自动生成的。(这是通过检查发送到服务器的TRACE确认的)。异常消息是:

Message =无法将值NULL插入表'RevLogon.dbo.SCIREPORTS'的列'FRID'中;列不允许为空。INSERT失败。该语句已终止。

我尝试使用流利的API来代替注释(并在注释之外)。

 modelBuilder.Entity<SCIREPORTS>()
            .HasKey(e => e.FRID);
 modelBuilder.Entity<SCIREPORTS>()
            .Property(e =>  e.FRID).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
Run Code Online (Sandbox Code Playgroud)

但是当我查询表时出现错误:

SCIREPORTS sr = mydb.SCIREPORTS.Where(s => s.FRID == FRID     ).FirstOrDefault();     
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

捕获到System.MissingMethodException HResult = -2146233069消息=未找到方法:“ System.Data.Entity.ModelConfiguration.Configuration.PrimitivePropertyConfiguration System.Data.Entity.ModelConfiguration.Configuration.PrimitivePropertyConfiguration.HasDatabaseGeneratedOption(System.Nullable`1)”。*

我已卸载并重新安装了EF软件包,但无法解决此问题。如何获得EF插入带有ID的记录?

这是插入记录的基本代码:

public SRUserComp(string myemail, short mycounter, int myFRID, string myreptype, string mypassword)
    {   email = myemail;
        Counter = mycounter;
        reptype = myreptype;
        password = mypassword;
        FRID = myFRID;        }

public bool CreateSRRecord(DateTime repduedate,
        short repnumber) …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework primary-key ef-code-first

5
推荐指数
0
解决办法
991
查看次数

未找到部分视图“/Views/Shared/ ...”或没有视图引擎支持搜索的位置

在 ASP.Net mvc cshtml 页面中,我有以下代码调用位于我的文件夹中的 4 个部分视图:

<div id="accordion">
     <h3 style="padding-left: 50px">Instructions</h3>
        <div>@Html.Partial("/Views/Shared/_instruct.cshtml", null)</div>

    <h3 style="padding-left: 50px">Projects by  @Html.DisplayFor(model => model.FirstName) @Html.DisplayFor(model => model.LastName)</h3>
        <div>@Html.Partial("~/Views/Shared/_research.cshtml", Model.Proposalsvm)</div>
   <h3 style="padding-left: 50px">  Publications</h3>
        <div>  @Html.Partial("~/Views/Shared/_publications.cshtml", Model.Pubs)</div>
    <h3 style="padding-left: 50px">  Patents</h3>
        <div>   @Html.Partial("~/Views/Shared/_patents.cshtml", Model.Patents)</div>
    <h3 style="padding-left: 50px">Training</h3>
        <div>@Html.Partial("~/Views/Shared/_Student.cshtml", Model.Students)</div>
    </div>
Run Code Online (Sandbox Code Playgroud)

所有视图都位于我的共享文件夹中,并且拼写正确。当我从本地计算机运行代码时,所有部分视图都会呈现。当我将代码部署到生产计算机时,如果注释掉第一个视图,则会呈现最后三个部分视图。如果不这样做,我会收到以下错误:

The partial view '/Views/Shared/_instruct.cshtml' was not found or no view engine supports the searched locations. The following locations were searched:
/Views/Shared/_instruct.cshtml 
Description: An unhandled exception occurred during the execution of the current web …
Run Code Online (Sandbox Code Playgroud)

c# model-view-controller asp.net-mvc razor

2
推荐指数
1
解决办法
8356
查看次数