Tag: foreign keys

Linq to SQL datacontext不更新外键关系

我正在针对使用L2S的存储库编写数据库测试。 在我的数据库中,我有一个Manifest实体和一个AllocatedTransaction实体。 AllocatedTransaction实体具有Manifest id的外键。 DDL看起来像这样: Manifest: Id – int – identity AllocateTransaction: Id – int – identity Quantity – int ManifestId – FK to Manifest 在我的测试中,我正在检查AllocatedTransactions是否与Manifest一起回来。 测试看起来像这样: [TestMethod] public void FindByIdTest() { using (data.EZTracDataContext dataContext = new data.EZTracDataContext()) { using (new TransactionScope()) { data.Manifest manifest = _testDataProvider.AddManifest(dataContext); data.AllocatedTransaction allocatedTransaction = _testDataProvider.AddEligibilityAllocated(dataContext, 5, manifest.Id); ManifestRepository repository […]

entity framework5:代码优先的周期性关系问题

我理解为什么EF不允许PK / FK关系中的“循环引用”。 我正在寻找有关如何更改模型以使以下方案有效的建议。 脚本 三个实体: Employee , Agency , WorkRecord 。 他们的目的是记录员工上class时间。 然后, Employee包含他/她所雇用的Agency的参考,并且他/她的工作WorkRecord包含对工作所完成的Agency的参考。 public class Employee { [Key] public int Id { get; set; } public string Name { get; set; } public int AgencyId { get; set; } public virtual Agency Agency { get; set; } public virtual IEnumerable WorkRecords { get; […]

具有可选要求的EF Code First + Fluent关系的多重约束违规?

出于某种原因,我让我在EF 6项目上做了一段时间,我会尽量避免命名外键。 我定义了大部分模型而没有逐步测试它,因此我遇到了多样性和不完整的Fluent API定义问题: 来自’User_InternalAuth’AssociationSet的关系处于’已删除’状态。 给定多重约束,相应的’User_InternalAuth_Target’也必须处于’已删除’状态。 在一种情况下,这是代码: nModelBuilder.Entity() .HasOptional(u => u.InternalAuth) .WithRequired(a => a.User) .WillCascadeOnDelete(true); 我的理解是它说: 实体User 具有InternalAuth类型的可选属性InternalAuth 另一方面, InternalAuth有一个必需的属性User ,所以所有的 InternalAuth都有User但是User可能有也可能没有`InternalAuth。 如果User被删除,那么他的InternalAuth也会被删除(这是否会覆盖处理像nullables这样的选项的可选行为?) 但是当我尝试删除User我收到一个关于InternalAuth和User之间某种关联的多样性的例外。 如果EF了解关系的多样性,它是否有一种方法可以为它提供一个唯一的列名,那么有一个规范的命名约定? 如果是这样,您是否真的需要通过注释模型或通过Fluent API明确定义外键? 如果没有,我应该继续努力避免它,这是值得的还是可取的? (我正在考虑迁移数据模型,数据库管理,任何EF怪癖) 为什么尝试删除上述关系会违反多重约束? 还需要知道什么?

entity framework不生成ApplicationUser外键

注意:对于这个问题,我稍微简化了模型。 我正在使用ASP.NET Core MVC应用程序(使用Entity Framework Core)。 我有三个型号。 我有一个ApplicationUser(从IdentityUser扩展),我有一个Activity模型,我有一个’SignUp’模型,它有一个活动的外键和ApplicationUser的外键。 至少,这是个主意。 问题是,Entity Framework将Activity FK识别为FK,而不是applicationuser。 这只是数据库中的另一个int而没有任何FK约束。 我尝试了很多在互联网上发现的东西,但我无法让它发挥作用。 这就是我目前所拥有的(为了清晰起见而缩短): 活动: public class Activity { public Activity() { SignUps = new HashSet(); } public int ActivityID { get; set; } [Required] [StringLength(50)] [Display(Name = “Naam”)] public string Name { get; set; } public virtual ICollection SignUps { get; set; } […]

entity framework一对多或一个外键关联

我正在更改现有应用程序的后端以使用Entity Framework Code First。 我已经使用Visual Studio 2015中的内置工具基于我现有的数据库生成POCO类。 这在大多数情况下都是完美的,除了两个类,一对一或零关系。 这些是我的(简化)课程: public class Login { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public int TeamMemberId { get; set; } public virtual TeamMember TeamMember { get; set; } } public class TeamMember { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int Id { get; set; } public virtual Login Login […]

如何首先在Entity Framework 5代码中使用两个外键创建主键?

我有一个实体,其中主键包含两个外键到另外两个表。 我有配置使用以下,但该表是使用两个FK引用生成的。 桌子: domain.Entity1 MorePK (PK, FK, int, not null) Entity2_Id (PK, FK, int, not null) Entity3_Id (PK, FK, int, not null) OtherData (varchar, null) Entity2_Id1 (FK, int, null) Entity3_Id1 (FK, int, null) 由以下内容生成: public Entity1 { public int MorePK { get; set; } public int Entity2_Id { get; set; } public int Entity3_Id { […]

如何使用Code First在ASP.NET MVC 5 Identity 2.0中的AspNetUser表(Id列)的Customer表(CreatedBy列)中添加外键

我使用Visual Studio 2013 Update 2 RC创建了Empty MVC(ASP.NET Web应用程序)项目,然后使用以下命令添加了AspNet Identity Samples: PM>安装包Microsoft.AspNet.Identity.Samples -Pre 我已启用并添加了迁移,然后更新了数据库,这些数据库创建了默认表。 我想创建一个Customer表,其中包含2列作为外键: 组表(GroupId列) AspNetUsers表(Id列) 所以我创建了2个Customer&Group类,并使用Data-annotations添加了外键,如下所示: namespace IdentitySample.Models { // You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more. public class ApplicationUser : IdentityUser { public async Task GenerateUserIdentityAsync(UserManager manager) { […]

无法更新entity framework6中的外键

我正在尝试对外键进行简单更新,但脚本永远不会被发送过来。 这是我正在使用的代码: using (var db = new MyContext()) { db.Entry(newContact).State = EntityState.Modified; newContact.ContactOwner = db.Person.Find(3); db.SaveChanges(); } EF6更新Persons表中的其余列,但它不更新Persons表中的Contact_Id。 人物实体: public class Person { public int Id { get; set; } public string Name { get; set; } public List ContactList { get; set; } } 联系实体: public class Contact { public int Id { get; […]

使用数据注释创建外键

在下面的代码中,我需要在ParentInfoAddProperties.ParentQuestionAnswersId上设置一个外键constrant,以便它依赖于ParentQuestionAnswers.Id(这是一个主键)。 我试图使用数据注释,但entity framework6想要在我的ParentQuestionAnswers表中创建一个新的外键列,该列引用ParentInfoAddProperties.Id列而不是ParentInfoAddProperties.ParentQuestionAnswersId列。 我不希望Entity Framework创建新的外键列。 如果有人能够解释我应该指定哪些数据注释或(如果需要)流畅的映射来实现所需的外键实例,我将不胜感激。 提前致谢。 namespace Project.Domain.Entities { public class ParentQuestionAnswers { public ParentQuestionAnswers() { ParentInfoAddProperties = new ParentInfoAddProperties(); } [Required] public int Id { get; set; } [Required] public int UserId { get; set; } public ParentInfoAddProperties ParentInfoAddProperties { get; set; } } public class ParentInfoAddProperties { [Required] public int Id { […]

如何使用FK设置集合属性?

我有一个Business和Category模型。 每个Business通过公开的集合拥有许多Categories ( Category无视Business实体)。 现在这是我的控制器动作: [HttpPost] [ValidateAntiForgeryToken] private ActionResult Save(Business business) { //Context is a lazy-loaded property that returns a reference to the DbContext //It’s disposal is taken care of at the controller’s Dispose override. foreach (var category in business.Categories) Context.Categories.Attach(category); if (business.BusinessId > 0) Context.Businesses.Attach(business); else Context.Businesses.Add(business); Context.SaveChanges(); return RedirectToAction(“Index”); } 现在有几个business.Categories将其CategoryId设置为现有Category ( Category的Title属性缺失)。 […]