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

时间:2022-10-05 15:19:01

I have an existing table (that is in SQL server) that I need to use in my solution that has no primary key. It has a unique field that it uses as the key but was never set up as the 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?

我有一个现有的表(在SQL server中),我需要在没有主键的解决方案中使用它。它有一个独特的字段,它作为钥匙使用,但从来没有设置为PK。我问我的老板,他说我们不能改变它。如何使用该字段作为主键,即使它没有标记为1 ?

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'. Could not create constraint. See previous errors.

在引用的表的dbo中没有主键或候选键。STCIProductInteractiveInfo'匹配外键“fk_dbo . pteinteractivecourses_dbo . stciproductinteractiveinfo_stciproductinteractiveinfo_interactiveinfoid”中的引用列列表。无法创建约束。见以前的错误。

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:

这是我的pteinteractiveccourse模型:

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; }
    }
}

1 个解决方案

#1


1  

If the field contains unique data you can mark it as Key in EF and disable all automatic migrations. It should work.

如果字段包含唯一的数据,可以在EF中将其标记为Key,并禁用所有自动迁移。它应该工作。

EDIT
You have to disable all the migrations because if EF tries to build missing objects it won't be able.
At the end you will not have the most performant database and also you need to implement referential integrity constraints by yourself.

编辑您必须禁用所有迁移,因为如果EF试图构建丢失的对象,它将无法。最后,您将不会拥有性能最好的数据库,而且您还需要自己实现引用完整性约束。

EDIT2
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). You should try to disable all the automatic migrations. Look here to do so How can I disable migration in Entity Framework 6.0

这里的问题是SQL Server,因为EF试图从pteinteractiveccourse创建关系。STCIProductInteractiveInfoID到STCIProductInteractiveInfo,它实际上没有主键(EF认为它有,但它不是真的)。您应该尝试禁用所有自动迁移。看这里,如何禁用Entity Framework 6.0中的迁移

#1


1  

If the field contains unique data you can mark it as Key in EF and disable all automatic migrations. It should work.

如果字段包含唯一的数据,可以在EF中将其标记为Key,并禁用所有自动迁移。它应该工作。

EDIT
You have to disable all the migrations because if EF tries to build missing objects it won't be able.
At the end you will not have the most performant database and also you need to implement referential integrity constraints by yourself.

编辑您必须禁用所有迁移,因为如果EF试图构建丢失的对象,它将无法。最后,您将不会拥有性能最好的数据库,而且您还需要自己实现引用完整性约束。

EDIT2
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). You should try to disable all the automatic migrations. Look here to do so How can I disable migration in Entity Framework 6.0

这里的问题是SQL Server,因为EF试图从pteinteractiveccourse创建关系。STCIProductInteractiveInfoID到STCIProductInteractiveInfo,它实际上没有主键(EF认为它有,但它不是真的)。您应该尝试禁用所有自动迁移。看这里,如何禁用Entity Framework 6.0中的迁移