【问题标题】:Having trouble with database first generated MVC 4 model with foreign keys数据库首先生成带有外键的 MVC 4 模型时遇到问题
【发布时间】:2014-01-14 10:07:56
【问题描述】:

多列指向同一个 People 表的表,即人员类

namespace IGRS.Models
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.ComponentModel.DataAnnotations.Schema;

    public partial class HinduMarriageForm
    {
        public int ID { get; set; }
        [ForeignKey("Person"), Column(Order=0)]
        public int HusbandID { get; set; }
        public int HusbandsFatherID { get; set; }
        public int HusbandsMotherID { get; set; }
        public int WifeID { get; set; }
        public int WifesFatherID { get; set; }
        public int WifesMotherID { get; set; }
        public Nullable<int> HusbandsNRIInfoID { get; set; }
        public Nullable<int> WifesNRIInfoID { get; set; }
        public Nullable<int> PersonWhoSolemnizedMarriageID { get; set; }
        public bool SolemnizedUnderSection7 { get; set; }
        public bool SolemnizedUnderSection7A { get; set; }
        public int MarriageVenueID { get; set; }
        public System.DateTime DateOfMarriage { get; set; }
        public int Witness1ID { get; set; }
        public int Witness2ID { get; set; }
        public int Witness3ID { get; set; }
        public int SROID { get; set; }
        public int SRID { get; set; }

        public virtual MarriageVenue MarriageVenue { get; set; }
        public virtual NRIInformation NRIInformation { get; set; }
        public virtual NRIInformation NRIInformation1 { get; set; }
        public virtual Person Person { get; set; }
        public virtual Person Person1 { get; set; }
        public virtual Person Person2 { get; set; }
        public virtual Person Person3 { get; set; }
        public virtual Person Person4 { get; set; }
        public virtual Person Person5 { get; set; }
        public virtual Person Person6 { get; set; }
        public virtual Person Person7 { get; set; }
        public virtual Person Person8 { get; set; }
        public virtual Person Person9 { get; set; }
        public virtual SR SR { get; set; }
        public virtual SRO SRO { get; set; }
    }
}

这是由数据库优先 MVC4 方法 Entity Framework 5 VS2012 默认自动生成的:

namespace IGRS.Models
{
    using System;
    using System.Collections.Generic;

    public partial class Person
    {
        public Person()
        {
            this.HinduMarriageForms = new HashSet<HinduMarriageForm>();
            this.HinduMarriageForms1 = new HashSet<HinduMarriageForm>();
            this.HinduMarriageForms2 = new HashSet<HinduMarriageForm>();
            this.HinduMarriageForms3 = new HashSet<HinduMarriageForm>();
            this.HinduMarriageForms4 = new HashSet<HinduMarriageForm>();
            this.HinduMarriageForms5 = new HashSet<HinduMarriageForm>();
            this.HinduMarriageForms6 = new HashSet<HinduMarriageForm>();
            this.HinduMarriageForms7 = new HashSet<HinduMarriageForm>();
            this.HinduMarriageForms8 = new HashSet<HinduMarriageForm>();
            this.HinduMarriageForms9 = new HashSet<HinduMarriageForm>();
        }

        public int ID { get; set; }
        public string FullName { get; set; }
        public Nullable<int> ReligionID { get; set; }
        public Nullable<int> CasteID { get; set; }
        public Nullable<System.DateTime> DateOfBirth { get; set; }
        public Nullable<int> MaritalStatusID { get; set; }
        public Nullable<int> OccupationID { get; set; }
        public string PermanentAddress { get; set; }
        public string PassportNumber { get; set; }

        public virtual Caste Caste { get; set; }
        public virtual ICollection<HinduMarriageForm> HinduMarriageForms { get; set; }
        public virtual ICollection<HinduMarriageForm> HinduMarriageForms1 { get; set; }
        public virtual ICollection<HinduMarriageForm> HinduMarriageForms2 { get; set; }
        public virtual ICollection<HinduMarriageForm> HinduMarriageForms3 { get; set; }
        public virtual ICollection<HinduMarriageForm> HinduMarriageForms4 { get; set; }
        public virtual ICollection<HinduMarriageForm> HinduMarriageForms5 { get; set; }
        public virtual ICollection<HinduMarriageForm> HinduMarriageForms6 { get; set; }
        public virtual ICollection<HinduMarriageForm> HinduMarriageForms7 { get; set; }
        public virtual ICollection<HinduMarriageForm> HinduMarriageForms8 { get; set; }
        public virtual ICollection<HinduMarriageForm> HinduMarriageForms9 { get; set; }
        public virtual MaritalState MaritalState { get; set; }
        public virtual Occupation Occupation { get; set; }
        public virtual Religion Religion { get; set; }
    }
}

以及 NRIInformation 表信息:

namespace IGRS.Models
{
    using System;
    using System.Collections.Generic;

    public partial class NRIInformation
    {
        public NRIInformation()
        {
            this.HinduMarriageForms = new HashSet<HinduMarriageForm>();
            this.HinduMarriageForms1 = new HashSet<HinduMarriageForm>();
        }

        public int ID { get; set; }
        public string PassportNumber { get; set; }
        public string PassportIssuedBy { get; set; }
        public System.DateTime PassportValidUpto { get; set; }
        public string VisaIssued { get; set; }
        public System.DateTime VisaValidUpto { get; set; }
        public int TypeOfResidentID { get; set; }
        public string SocialSecurityNumber { get; set; }

        public virtual ICollection<HinduMarriageForm> HinduMarriageForms { get; set; }
        public virtual ICollection<HinduMarriageForm> HinduMarriageForms1 { get; set; }
    }
}

这在 HinduMarriageForm 控制器代码上运行时很明显,因此不提供会给出错误:

“/”应用程序中的服务器错误。

列名“NRIInformation_ID”无效。 列名“NRIInformation_ID1”无效。 列名“NRIInformation_ID2”无效。 列名“NRIInformation1_ID”无效。 列名“Person_ID”无效。 列名“Person_ID1”无效。 列名“Person_ID2”无效。 列名“Person_ID3”无效。 列名“Person_ID4”无效。 列名“Person_ID5”无效。 列名“Person_ID6”无效。 列名“Person_ID7”无效。 列名“Person_ID8”无效。 列名“Person_ID9”无效。 列名“Person1_ID”无效。 列名“Person2_ID”无效。 列名“Person3_ID”无效。 列名“Person4_ID”无效。 列名“Person5_ID”无效。 列名“Person6_ID”无效。 列名“Person7_ID”无效。 列名“Person8_ID”无效。 列名“Person9_ID”无效。

说明:在执行当前 Web 请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.Data.SqlClient.SqlException:列名“NRIInformation_ID”无效。 列名“NRIInformation_ID1”无效。 列名“NRIInformation_ID2”无效。 列名“NRIInformation1_ID”无效。 列名“Person_ID”无效。 列名“Person_ID1”无效。 列名“Person_ID2”无效。 列名“Person_ID3”无效。 列名“Person_ID4”无效。 列名“Person_ID5”无效。 列名“Person_ID6”无效。 列名“Person_ID7”无效。 列名“Person_ID8”无效。 列名“Person_ID9”无效。 列名“Person1_ID”无效。 列名“Person2_ID”无效。 列名“Person3_ID”无效。 列名“Person4_ID”无效。 列名“Person5_ID”无效。 列名“Person6_ID”无效。 列名“Person7_ID”无效。 列名“Person8_ID”无效。 列名“Person9_ID”无效。

来源错误:

第 20 行:{ 第 21 行: var HinduMarriageforms = db.HinduMarriageForms.Include(h => h.MarriageVenue).Include(h => h.SR).Include(h => h.SRO); 第 22 行:返回 View(hindumarriageforms.ToList()); 第 23 行:} 第 24 行:

源文件:c:\Users\gilgamesh\Documents\Visual Studio 2012\Projects\IGRS\IGRS\Controllers\HinduMarriageFormController.cs 行:22

堆栈跟踪:

[SqlException (0x80131904):列名“NRIInformation_ID”无效。 列名“NRIInformation_ID1”无效。 列名“NRIInformation_ID2”无效。 列名“NRIInformation1_ID”无效。 列名“Person_ID”无效。 列名“Person_ID1”无效。 列名“Person_ID2”无效。 列名“Person_ID3”无效。 列名“Person_ID4”无效。 列名“Person_ID5”无效。 列名“Person_ID6”无效。 列名“Person_ID7”无效。 列名“Person_ID8”无效。 列名“Person_ID9”无效。 列名“Person1_ID”无效。 列名“Person2_ID”无效。 列名“Person3_ID”无效。 列名“Person4_ID”无效。 列名“Person5_ID”无效。 列名“Person6_ID”无效。 列名“Person7_ID”无效。 列名“Person8_ID”无效。 列名“Person9_ID”无效。] System.Data.SqlClient.SqlConnection.OnError(SqlException异常,布尔breakConnection,Action1 wrapCloseInAction) +1767866 System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action1 wrapCloseInAction)+5352418 System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj,布尔调用者HasConnectionLock,布尔异步关闭)+244 System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +1691 System.Data.SqlClient.SqlDataReader.TryConsumeMetaData() +61 System.Data.SqlClient.SqlDataReader.get_MetaData() +90 System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds,RunBehavior runBehavior,字符串 resetOptionsString)+365 System.Data.SqlClient.SqlCommand.RunExecuteReaderTds(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,Boolean async,Int32 超时,Task& 任务,Boolean asyncWrite,SqlDataReader ds)+1406 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior,RunBehavior runBehavior,Boolean returnStream,String 方法,TaskCompletionSource`1 完成,Int32 超时,Task& 任务,Boolean asyncWrite)+177 System.Data.SqlClient.SqlCommand.RunExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream, String method) +53 System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior 行为,字符串方法)+134 System.Data.SqlClient.SqlCommand.ExecuteDbDataReader(CommandBehavior 行为)+41 System.Data.Common.DbCommand.ExecuteReader(CommandBehavior 行为)+10 System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand, CommandBehavior 行为) +437

[EntityCommandExecutionException:执行命令定义时出错。有关详细信息,请参阅内部异常。] System.Data.EntityClient.EntityCommandDefinition.ExecuteStoreCommands(EntityCommand entityCommand,CommandBehavior 行为)+507 System.Data.Objects.Internal.ObjectQueryExecutionPlan.Execute(ObjectContext context, ObjectParameterCollection parameterValues) +730 System.Data.Objects.ObjectQuery1.GetResults(Nullable1 forMergeOption) +131 System.Data.Objects.ObjectQuery1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() +36 System.Data.Entity.Internal.Linq.InternalQuery1.GetEnumerator() +126 System.Data.Entity.Infrastructure.DbQuery1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator() +99 System.Collections.Generic.List1..ctor(IEnumerable1 collection) +369 System.Linq.Enumerable.ToList(IEnumerable1 源) +58 c:\Users\gilgamesh\Documents\Visual Studio 2012\Projects\IGRS\IGRS\Controllers\HinduMarriageFormController.cs:22 中的 IGRS.Controllers.HinduMarriageFormController.Index() lambda_method(闭包,ControllerBase,对象[])+101 System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase 控制器,Object[] 参数)+14 System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) +211 System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary2 参数) +27 System.Web.Mvc.Async.c_DisplayClass42.b_41() +28 System.Web.Mvc.Async.c_DisplayClass81.<BeginSynchronous>b__7(IAsyncResult _) +10 System.Web.Mvc.Async.WrappedAsyncResult1.End() +57 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +48 System.Web.Mvc.Async.c_DisplayClass39.b_33() +57 System.Web.Mvc.Async.c_DisplayClass4f.b_49() +223 System.Web.Mvc.Async.c_DisplayClass37.b_36(IAsyncResult asyncResult) +10 System.Web.Mvc.Async.WrappedAsyncResult1.End() +57 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +48 System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +24 System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +102 System.Web.Mvc.Async.WrappedAsyncResult1.End() +57 System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +43 System.Web.Mvc.c_DisplayClass1d.b_18(IAsyncResult asyncResult) +14 System.Web.Mvc.Async.c_DisplayClass4.b_3(IAsyncResult ar) +23 System.Web.Mvc.Async.WrappedAsyncResult1.End() +62 System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +57 System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +23 System.Web.Mvc.Async.WrappedAsyncResult1.End() +62 System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +47 System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +10 System.Web.Mvc.c_DisplayClass8.b_3(IAsyncResult asyncResult) +25 System.Web.Mvc.Async.c_DisplayClass4.b__3(IAsyncResult ar) +23 System.Web.Mvc.Async.WrappedAsyncResult`1.End() +62 System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +47 System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult 结果) +9 System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9514812 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

版本信息:Microsoft .NET Framework 版本:4.0.30319; ASP.NET 版本:4.0.30319.18408

到目前为止,我已经尝试了很多使用 ForeignKey 和 Column 以及 Order 装饰等的东西,但只能减少错误的数量。我不确定反射出了什么问题。这里还有 2 个外键表和源表的 SQL 创建表脚本:

CREATE TABLE [dbo].[HinduMarriageForms](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [HusbandID] [int] NOT NULL,
    [HusbandsFatherID] [int] NOT NULL,
    [HusbandsMotherID] [int] NOT NULL,
    [WifeID] [int] NOT NULL,
    [WifesFatherID] [int] NOT NULL,
    [WifesMotherID] [int] NOT NULL,
    [HusbandsNRIInfoID] [int] NULL,
    [WifesNRIInfoID] [int] NULL,
    [PersonWhoSolemnizedMarriageID] [int] NULL,
    [SolemnizedUnderSection7] [bit] NOT NULL,
    [SolemnizedUnderSection7A] [bit] NOT NULL,
    [MarriageVenueID] [int] NOT NULL,
    [DateOfMarriage] [datetime] NOT NULL,
    [Witness1ID] [int] NOT NULL,
    [Witness2ID] [int] NOT NULL,
    [Witness3ID] [int] NOT NULL,
    [SROID] [int] NOT NULL,
    [SRID] [int] NOT NULL,
 CONSTRAINT [PK_HinduMarriageForm] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

CREATE TABLE [dbo].[NRIInformations](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [PassportNumber] [nvarchar](50) NOT NULL,
    [PassportIssuedBy] [nvarchar](255) NOT NULL,
    [PassportValidUpto] [datetime] NOT NULL,
    [VisaIssued] [nvarchar](255) NOT NULL,
    [VisaValidUpto] [datetime] NOT NULL,
    [TypeOfResidentID] [int] NOT NULL,
    [SocialSecurityNumber] [nchar](10) NULL,
 CONSTRAINT [PK_NRIInformation] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

CREATE TABLE [dbo].[People](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [FullName] [nvarchar](255) NOT NULL,
    [ReligionID] [int] NULL,
    [CasteID] [int] NULL,
    [DateOfBirth] [datetime] NULL,
    [MaritalStatusID] [int] NULL,
    [OccupationID] [int] NULL,
    [PermanentAddress] [nvarchar](512) NOT NULL,
    [PassportNumber] [nvarchar](50) NULL,
 CONSTRAINT [PK_Person] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

不粘贴外键 SQL 创建语句假设它们都在后端 sql server 数据库中。

整个 MVC 4 Entity Framework 5 项目是 Database First Driven 并给出了这些错误。没有多个列链接到相同外键表和列的更简单的表工作正常。

【问题讨论】:

  • 找到这个 stackoverflow 评论但无法应用它,因为它假设指向的对象/行是相同的,这在我的情况下不是,因为它们是不同的人/人/行。这是另一个 stackoverflow 问答的链接:stackoverflow.com/questions/5436731/…

标签: asp.net asp.net-mvc-4 frameworks ado.net entity


【解决方案1】:

来自您的错误消息的这一部分:

    Source Error:

Line 20: { Line 21: var hindumarriageforms = db.HinduMarriageForms.Include(h => h.MarriageVenue).Include(h => h.SR).Include(h => h.SRO); Line 22: return View(hindumarriageforms.ToList()); Line 23: } Line 24:

Source File: c:\Users\gilgamesh\Documents\Visual Studio 2012\Projects\IGRS\IGRS\Controllers\HinduMarriageFormController.cs Line: 22

如果您还包括 NRIInformation 和 Person 表,问题会消失吗?

var hindumarriageforms = db.HinduMarriageForms.Include(h => h.MarriageVenue).Include(h => h.SR).Include(h => h.SRO).Include(h => h.Person).Include(h => h.NRIInformation);

编辑:顺便说一句,我通常喜欢重命名虚拟对象,而不是 Person1、Person2、Person3... 等。将其更改为 PersonForHusband、PersonForHusbandsFather、PersonForHusbandsMother 等导航属性清晰,您不会意外使用错误的属性。使以后使用这些类进行编程变得更容易使用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-26
    • 2023-03-14
    • 1970-01-01
    • 1970-01-01
    • 2017-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多