简体   繁体   English

在没有主键的实体中使用表(使用实体代码优先迁移)

[英]Use Table in Entity with no Primary Key (Using Entity Code-first migrations)

I have an existing table (that is in SQL server) that I need to use in my solution that has no primary key. 我有一个需要在我的解决方案中使用的没有主键的现有表(在SQL Server中)。 It has a unique field that it uses as the key but was never set up as the PK. 它有一个唯一的字段用作键,但从未设置为PK。 I asked my boss and he said we cannot change it. 我问我的老板,他说我们不能改变它。 How do I use that field as a primary key, even though it is not labeled as one? 即使未将其标记为一个字段,如何将其用作主键?

I am using code first migrations. 我正在使用代码优先迁移。

I found this when searching? 我在搜索时发现了这个? but all the answers refer to a view, not a table. 但是所有答案都指向一个视图,而不是一个表。

Entity Framework: table without primary key 实体框架:不带主键的表

here is my model: 这是我的模型:

public partial class STCIProductInteractiveInfo
    {

        public Guid STCIProductInteractiveInfoID { get; set; }

        public Guid MarketingTextID { get; set; }

        public DateTime ModifyDate { get; set; }

        public int ModifyUserID { get; set; }

        public string STCICourseID { get; set; }

        public string STCICourseName { get; set; }

        public byte STCIProductEmailHTML { get; set; }

        public string STCIProductEmailHTMLDateType { get; set; }

        public int STCIProductEmailHTMLFileLength { get; set; }

        public byte STCIProductEmailText { get; set; }

        public string STCIProductEmailTextDataType { get; set; }

        public string STCIProductEmailTextFileLength { get; set; }

        public string STCITrackingClassCode { get; set; }

        public string STCITrackingClassName { get; set; }

        public int u_product_id { get; set; }

    }

After marking STCIProductInteractiveInfoID like this: 像这样标记STCIProductInteractiveInfoID之后:

[Key]
public Guid STCIProductInteractiveInfoID { get; set; }

I am getting this error: 我收到此错误:

There are no primary or candidate keys in the referenced table 'dbo.STCIProductInteractiveInfo' that match the referencing column list in the foreign key 'FK_dbo.PTEInteractiveCourses_dbo.STCIProductInteractiveInfo_STCIProductInteractiveInfoID'. 在引用表'dbo.STCIProductInteractiveInfo'中没有与外键'FK_dbo.PTEInteractiveCourses_dbo.STCIProductInteractiveInfo_STCIProductInteractiveInfoID'中的引用列列表匹配的主键或候选键。 Could not create constraint. 无法创建约束。 See previous errors. 请参阅先前的错误。

I have put this code in my DataContext: 我已将此代码放在DataContext中:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {

            // Add Primary key for STCIProductInteractiveInfo
            base.OnModelCreating(modelBuilder);
            modelBuilder.Entity<STCIProductInteractiveInfo>().HasKey(x => new { x.STCIProductInteractiveInfoID });
        }

and as requested here is my PTEInteractiveCourse model: 这是我的PTEInteractiveCourse模型所要求的:

namespace PTEManager.Domain
{
    public class PTEInteractiveCourse
    {
        [Key]
        public Guid PTEInteractiveCourseId { get; set; }

        [ScaffoldColumn(false)]
        [Required]
        public DateTime ModifyDate { get; set; }

        [ScaffoldColumn(false)]
        [Display(Name = "")]
        [Required]
        public int Status { get; set; }

        [Display(Name = "STCI Course")]
        [ForeignKey("STCICourseName")]
        [Required]
        public Guid STCIProductInteractiveInfoID { get; set; }

        public virtual STCIProductInteractiveInfo STCICourseName { get; set; }
    }
}

If the field contains unique data you can mark it as Key in EF and disable all automatic migrations. 如果该字段包含唯一数据,则可以在EF中将其标记为Key,并禁用所有自动迁移。 It should work. 它应该工作。

EDIT 编辑
You have to disable all the migrations because if EF tries to build missing objects it won't be able. 您必须禁用所有迁移,因为如果EF尝试构建丢失的对象,它将无法执行。
At the end you will not have the most performant database and also you need to implement referential integrity constraints by yourself. 最后,您将没有性能最高的数据库,而且您需要自己实现引用完整性约束。

EDIT2 编辑2
The problem here is SQL Server because EF is trying to create the relationship from PTEInteractiveCourse.STCIProductInteractiveInfoID to STCIProductInteractiveInfo that does not really have a primary key (EF thinks it has but it is not true). 这里的问题是SQL Server,因为EF试图创建从PTEInteractiveCourse.STCIProductInteractiveInfoID到STCIProductInteractiveInfo的关系,该关系没有真正的主键(EF认为它具有主键,但事实并非如此)。 You should try to disable all the automatic migrations. 您应该尝试禁用所有自动迁移。 Look here to do so How can I disable migration in Entity Framework 6.0 查看此处以执行操作如何在Entity Framework 6.0中禁用迁移

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 首先使用Entity Framework代码创建复杂的主键 - Create a complex Primary Key using Entity Framework code-first 实体框架代码优先设置的外键和同一表中的主键 - Entity Framework code-first set foreign key with primary key in same table Entity Framework Core 2.1,使用代码优先迁移重命名表而不删除和创建新表 - Entity Framework Core 2.1, rename table using code-first migrations without dropping and creating new table 更正注释以在Entity Framework代码优先中设置主键 - Correct annotation to set primary key in Entity Framework code-first 实体框架6代码优先的迁移生成 - Entity Framework 6 code-first migrations generation 实体框架,代码优先,如何从多对多关系映射中向表添加主键? - Entity Framework, code-first, how to add primary key to table out of many-to-many relationship mapping? 实体框架代码优先的多对多表外键 - Entity Framework code-first foreign key to many to many table 在EF4代码优先中,如何使一个实体的主键成为另一个实体? - How can I have the primary key of one entity be another entity in EF4 code-first? 如何使用Entity Framework代码优先映射带有复合键的继承表? - How do I map an inherited table with a composite key using Entity Framework code-first? 实体框架代码优先迁移不生成外键并忽略 ForeignKey 数据注释 - Entity Framework code-first migrations does not generate a foreign key and ignores the ForeignKey data annotation
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM