【发布时间】:2016-05-21 07:02:21
【问题描述】:
我是 MVC 的新手,我在 Visual Studio 2015 中将 MVC 5 用于 ASP .NET C# Web 应用程序。
代码优先。
我的目标:在代码中定义外键关系,让脚手架在数据库中创建键。
我很难做到这一点。如果可能的话,我宁愿不使用Fluent API。至少,我想了解 1) 为什么我需要使用 Fluent API,以及 2) 实际上如何使用它。
模型
ApplicationType 模型
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace MVCModelFirstSample.Models
{
public class ApplicationType
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int ApplicationTypeID { get; set; }
[Required]
[MaxLength(50)]
public string ApplicationTypeShortDescription { get; set; }
[Required]
[MaxLength(500)]
public string ApplicationTypeLongDescription { get; set; }
[Required]
public int CreatedByUserID { get; set; }
[Required]
[DataType(DataType.Date)]
public DateTime CreatedDate { get; set; }
[Required]
public int ModifiedByUserID { get; set; }
[Required]
[DataType(DataType.Date)]
public DateTime ModifiedDate { get; set; }
}
}
现在,我希望创建一些使用ApplicationTypeID 作为外键的其他类。我已经创建了一个:
MVC5应用程序
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace MVCModelFirstSample.Models
{
public class MVC5Application
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int MVC5ApplicationID { get; set; }
[Required]
public int ApplicationTypeID { get; set; }
[Required]
[MaxLength(500)]
public string MVC5ApplicationDescription { get; set; }
[Required]
public int CreatedByUserID { get; set; }
[Required]
[DataType(DataType.Date)]
public DateTime CreatedDate { get; set; }
[Required]
public int ModifiedByUserID { get; set; }
[Required]
[DataType(DataType.Date)]
public DateTime ModifiedDate { get; set; }
}
}
就其本身而言,这些模型可以工作,我可以构建控制器,并且可以正确生成数据库等。但是,数据库的 MVC5Application 表中的 ApplicationTypeID 和 ApplicationType 表之间没有链接。
根据我的一些研究,有迹象表明可能会创建自动生成的外键关系,但尚不清楚。没有。
所以,我的第一个想法是在MVCApplication 类的属性定义上方添加一个[ForeignKey("ApplicationTypeID")] 属性。
对我来说,这样做是有道理的(实际上是否明智是另一回事)。无论如何,sn-p:
namespace MVCModelFirstSample.Models
{
public class MVC5Application
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int MVC5ApplicationID { get; set; }
[Required]
[ForeignKey("ApplicationTypeID")]
public int ApplicationTypeID { get; set; }
当我尝试通过创建控制器操作添加新的 MVCApplication 时,它会构建:
“System.InvalidOperationException”类型的异常发生在 EntityFramework.dll 但未在用户代码中处理 附加 信息:无法将属性“ApplicationTypeID”配置为 导航属性。该属性必须是有效的实体类型,并且 该属性应该有一个非抽象的 getter 和 setter。为了 集合属性该类型必须实现 ICollection 其中 T 是有效的实体类型。
这就是我感到困惑的地方。我发誓,我已经搜索了又搜索了又搜索了。我找到了不同的建议,但我不清楚如何设置它。
一种可能的选择是将类声明为键。我想,也许,它会自动检测类中的 ID 并使用它?不确定,但是嘿,值得一试,对吧?似乎你几乎可以在这些字符串值中放入任何你想要的东西。在使用之前没有人会抱怨。
public class MVC5Application
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int MVC5ApplicationID { get; set; }
[Required]
[ForeignKey("ApplicationType")]
public int ApplicationTypeID { get; set; }
错误:
“System.InvalidOperationException”类型的异常发生在 EntityFramework.dll 但未在用户代码中处理 附加 信息:属性“ApplicationTypeID”上的 ForeignKeyAttribute 'MVCModelFirstSample.Models.MVC5Application' 类型无效。 在 依赖类型“MVCModelFirstSample.Models.MVC5Application”。名字 value 应该是一个有效的导航属性名称。
这可能不是最好的主意。
是否甚至可以通过代码优先通过定义类并提供外键来生成外键关系?我在这里尽量保持简单。
理想情况下,我希望使用具有不一定匹配的数据库列名的键。例如,我有一个 User 表,其 UserID 值与其他表中的 CreatedByUserID 相关。
话虽如此,我引出了 Fluent API。
我遇到了麻烦。我需要查找和使用的 DbContext 到底在哪里?我想我终于想通了。
因为我使用了 Internet 身份验证,所以我有 IdentityModel.cs,其中包含以下内容:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false)
{
}
...然后我将添加我的覆盖:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public ApplicationDbContext() : base("DefaultConnection", throwIfV1Schema: false)
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
在覆盖中,我将添加我的 Fluent API。
根据Configuring Relationships with the Fluent API:
重命名模型中未定义的外键
如果您选择不在 CLR 类型上定义外键,但想要 指定它应该在数据库中具有什么名称,请执行以下操作:
modelBuilder.Entity<Course>()
.HasRequired(c => c.Department)
.WithMany(t => t.Courses)
.Map(m => m.MapKey("ChangedDepartmentID"));
没有。我无法直接使用此代码。我要做的就是坐下来查看示例类并将它们与我自己的类联系起来。我意识到我对这些东西应该如何工作存在一些根本性的误解或理解。
我不得不这样修改我的课程:
public class ApplicationType
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int ApplicationTypeID { get; set; }
[Required]
[MaxLength(50)]
public string ApplicationTypeShortDescription { get; set; }
[Required]
[MaxLength(500)]
public string ApplicationTypeLongDescription { get; set; }
[Required]
public int CreatedByUserID { get; set; }
[Required]
[DataType(DataType.Date)]
public DateTime CreatedDate { get; set; }
[Required]
public int ModifiedByUserID { get; set; }
[Required]
[DataType(DataType.Date)]
public DateTime ModifiedDate { get; set; }
public virtual ICollection<MVC5Application> Applications { get; set; }
}
public class MVC5Application
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int MVC5ApplicationID { get; set; }
//[Required]
//[ForeignKey("ApplicationType")]
//public int ApplicationTypeID { get; set; }
public virtual ApplicationType ApplicationType { get; set; }
[Required]
[MaxLength(500)]
public string MVC5ApplicationDescription { get; set; }
[Required]
public int CreatedByUserID { get; set; }
[Required]
[DataType(DataType.Date)]
public DateTime CreatedDate { get; set; }
[Required]
public int ModifiedByUserID { get; set; }
[Required]
[DataType(DataType.Date)]
public DateTime ModifiedDate { get; set; }
}
现在,按照 Fluent API 文章中概述的模式:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<MVC5Application>()
.HasRequired(r => r.ApplicationType)
.WithMany(w => w.Applications)
.Map(m => m.MapKey("ApplicationTypeID"));
base.OnModelCreating(modelBuilder);
}
经过大量迁移 (Code First Migrations) 工作:
我得到了我想要的结果,我想:
但是,我必须说实话。在这一点上,整个过程相当不直观。
有没有更好的办法?
另外,如果有人对使用代码优先创建数据库表有任何其他参考,以便更容易理解,我将不胜感激。
谢谢
【问题讨论】:
-
Fluent API 比属性方法更强大。您可以在强类型类中的一个位置进行配置。
-
如果您在 MVC5Application 中创建 ApplicationType 类型的属性,这将为您创建关系。在类结构中,您不会为关系创建属性整数,而只是相关数据的实例。
-
哎呀,有很多信息。一些基础知识,EF 可以通过约定、注释或流利的方式建立关系。我喜欢流利的,所以我可以把我的顾虑分开。一个明显的错误是您使用 ForeignKey 属性 - 您没有将其分配给外键 id - 您将其分配给导航属性,该属性是类本身。 [ForeignKey("ApplicationTypeID")] public ApplicationType ApplicationType { get;放; }
标签: c# asp.net .net asp.net-mvc entity-framework