使用表中的实体,没有主键(使用实体code-先迁移) [英] Use Table in Entity with no Primary Key (Using Entity Code-first migrations)

查看:262
本文介绍了使用表中的实体,没有主键(使用实体code-先迁移)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个现有的表(​​也就是在SQL服务器),我需要在我的解决方案,没有主键使用。它有它使用为重点,但从未设立的PK一个唯一的字段。我问我的老板,他说,我们不能改变它。我如何使用该字段作为主键,即使它没有打成一个?

我使用code首先迁移。

我发现这个搜索时?但所有的答案引用视图,而不是表

实体框架:不带表的主键

这里是我的模型:

 公共部分类STCIProductInteractiveInfo
    {        公众的Guid STCIProductInteractiveInfoID {搞定;组; }        公众的Guid MarketingTextID {搞定;组; }        公众的DateTime ModifyDate {搞定;组; }        公众诠释ModifyUserID {搞定;组; }        公共字符串STCICourseID {搞定;组; }        公共字符串STCICourseName {搞定;组; }        公共字节STCIProductEmailHTML {搞定;组; }        公共字符串STCIProductEmailHTMLDateType {搞定;组; }        公众诠释STCIProductEmailHTMLFileLength {搞定;组; }        公共字节STCIProductEmailText {搞定;组; }        公共字符串STCIProductEmailTextDataType {搞定;组; }        公共字符串STCIProductEmailTextFileLength {搞定;组; }        公共字符串STCITrackingClass code {搞定;组; }        公共字符串STCITrackingClassName {搞定;组; }        公众诠释u_product_id {搞定;组; }    }

标记STCIProductInteractiveInfoID这样之后:

  [重点]
公众的Guid STCIProductInteractiveInfoID {搞定;组; }

我得到这个错误:


  

有在引用表上没有主或候选键
  dbo.STCIProductInteractiveInfo'匹配引用列
  列表中的外键
  FK_dbo.PTEInteractiveCourses_dbo.STCIProductInteractiveInfo_STCIProductInteractiveInfoID。
  无法创建约束。见previous错误。


我已经把这个code在我的DataContext:

 保护覆盖无效OnModelCreating(DbModelBuilder模型构建器)
        {            //添加STCIProductInteractiveInfo主键
            base.OnModelCreating(模型构建器);
            modelBuilder.Entity< STCIProductInteractiveInfo方式>()HasKey(X =>新建{x.STCIProductInteractiveInfoID});
        }

和这里要求的是我的PTEInteractiveCourse模型:

 命名空间PTEManager.Domain
{
    公共类PTEInteractiveCourse
    {
        [键]
        公众的Guid PTEInteractiveCourseId {搞定;组; }        [ScaffoldColumn(假)]
        [需要]
        公众的DateTime ModifyDate {搞定;组; }        [ScaffoldColumn(假)]
        [显示(名称=)]
        [需要]
        公众诠释状态{搞定;组; }        [显示(NAME =STCI课程)]
        [ForeignKey的(STCICourseName)]
        [需要]
        公众的Guid STCIProductInteractiveInfoID {搞定;组; }        公共虚拟STCIProductInteractiveInfo STCICourseName {搞定;组; }
    }
}


解决方案

如果该字段包含独特的数据可以将其标记为EF Key和禁用所有的自动迁移。它应该工作。

修改结果
你必须禁用所有的迁移,因为如果EF尝试构建缺少的对象,它不能。结果
在结束时,你将不会有最高效的数据库,你也需要自己实现参照完整性约束。

EDIT2 结果
这里的问题是SQL Server的,因为EF试图创建PTEInteractiveCourse.STCIProductInteractiveInfoID到STCIProductInteractiveInfo的关系,并没有真正有一个主键(EF认为它有,但事实并非如此)。你应该尝试禁用所有的自动迁移。看看这里这样做<一个href=\"http://stackoverflow.com/questions/18667172/how-can-i-disable-migration-in-entity-framework-6-0\">How我可以在实体框架禁用迁移6.0

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?

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:

[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.

I have put this code in my 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:

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. It should work.

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.

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

这篇关于使用表中的实体,没有主键(使用实体code-先迁移)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆