错误0019:类型中的每个属性名称在ID字段上必须唯一 [英] error 0019: Each property name in a type must be unique on ID field

查看:282
本文介绍了错误0019:类型中的每个属性名称在ID字段上必须唯一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ANSWERED!(即将发布.我认真地认为这是EF中的错误.)

ANSWERED! (will be posting it shortly. I seriously think it's a bug in EF.)

我有以下(代码优先)类集,这将是应用程序的未来,因为我们将替换旧的(从db中生成). dbcontext,两个映射/配置,两个POCO和一个基类.

I have the following set of (code-first) classes which will be the future of the application as we are replacing the old (gen'd from db). The dbcontext, two mappings/configurations, two POCOs, and a base class.

此外,在另一个项目中,完全是从数据库生成的edmx.到现在为止,这两个冲突还没有任何问题.上下文中还有其他几个不相关的集合(未显示),但是直到我开始尝试获取SalesRepsSalesGroups之间的关系时,我才开始遇到问题.注释掉在OnModelCreating中添加SalesGroupMapping将会使其运行.

In addition to this in another project entirely is an edmx generated from the database. Up until now, there have not been any issues with the two conflicting. There are several other unrelated sets in the context (not shown,) but it wasn't until I started trying to get the relationship between SalesReps and SalesGroups that I started having issues. Commenting out adding the SalesGroupMapping in OnModelCreating will allow it to run.

错误:

Schema specified is not valid. Errors:
(19,6) : error 0019: Each property name in a type must be unique. Property name 'PKID_MasterRepGroup' was already defined.
(27,6) : error 0019: Each property name in a type must be unique. Property name 'PKID_MasterRepGroup' was already defined.

代码:

public class MyDbContext{
public IDbSet<SalesGroup> SalesGroups { get; set; }


            public IDbSet<SalesRep> SalesReps { get; set; }
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
                        {
                            modelBuilder.Configurations.Add(new InsuranceProviderMapping());
                            modelBuilder.Configurations.Add(new SalesGroupMapping());
                            modelBuilder.Configurations.Add(new SalesRepMapping());
                            base.OnModelCreating(modelBuilder);

                        }
}

        class SalesRepMapping : EntityTypeConfiguration<SalesRep>
            {
                public SalesRepMapping()
                {
                    ToTable("SalesRep");
                    Property(p => p.Id).HasColumnName("PKID_SalesRep");

                    Property(p => p.Email).HasColumnName("EMailAddress1");

                    Property(p => p.Address.Address1).HasColumnName("Address");
                    Property(p => p.Address.Address2).HasColumnName("Address2");
                    Property(p => p.Address.City).HasColumnName("City");
                    Property(p => p.Address.State).HasColumnName("State");
                    Property(p => p.Address.Zipcode).HasColumnName("Zipcode");
                    //Property(p => p.SalesGroupId).HasColumnName("FK_MasterRepGroup");
                    //HasRequired(p => p.SalesGroup).WithMany(c => c.SalesReps).HasForeignKey(b => b.SalesGroupId);

                    //Note: dropping emailaddress2
                }
            }

            class SalesGroupMapping : EntityTypeConfiguration<SalesGroup>
                {
                    public SalesGroupMapping()
                    {
                        ToTable("MasterRepGroup");
                        Property(p => p.Id).HasColumnName("PKID_MasterRepGroup");
                        Property(p => p.Name).HasColumnName("CompanyName");
                        Property(p => p.PrimaryContact.FirstName).HasColumnName("Contact1FirstName");
                        Property(p => p.PrimaryContact.LastName).HasColumnName("Contact1LastName");
                        Property(p => p.PrimaryContact.Phone).HasColumnName("Contact1Phone");
                        Property(p => p.PrimaryContact.Fax).HasColumnName("Contact1Fax");
                        Property(p => p.PrimaryContact.Email).HasColumnName("Contact1EMail");
                        Property(p => p.SecondaryContact.FirstName).HasColumnName("Contact2FirstName");
                        Property(p => p.SecondaryContact.LastName).HasColumnName("Contact2LastName");
                        Property(p => p.SecondaryContact.Phone).HasColumnName("Contact2Phone");
                        Property(p => p.SecondaryContact.Fax).HasColumnName("Contact2Fax");
                        Property(p => p.SecondaryContact.Email).HasColumnName("Contact2EMail");
                        Property(p => p.Address.Address1).HasColumnName("Address");
                                    Property(p => p.Address.Address2).HasColumnName("Address2");
                        Property(p => p.Address.City).HasColumnName("City");
                        Property(p => p.Address.State).HasColumnName("State");
                        Property(p => p.Address.Zipcode).HasColumnName("Zipcode");
                    }
                }

    public class SalesRep: Entity
        {

            virtual public string FirstName { get; set; }
            [Required]
            public string LastName { get; set; }
            [Phone]
            virtual public string Phone { get; set; }
            [Phone]
            virtual public string Fax { get; set; }
            [Email]
            virtual public string Email { get; set; }
            virtual public string Notes { get; set; }

            virtual public Address Address { get; set; }

            //public long SalesGroupId { get; set; }

            //[Required]
            ////[ForeignKey("SalesGroupId")]
            //virtual public SalesGroup SalesGroup { get; set; }
        }

public class SalesGroup : Entity
    {
        public SalesGroup()
        {
            //SalesReps = new List<SalesRep>();
        }

        [Required]
        public string Name { get; set; }

        virtual public Address Address { get; set; }
        virtual public Contact PrimaryContact { get; set; }
        virtual public Contact SecondaryContact { get; set; }

        //virtual public ICollection<SalesRep> SalesReps {get; set;}
    }

        public abstract class Entity : NMTC.Core.DataContracts.IEntity
            {

                // TODO database columns will eventually be int instead of bigint
                public long Id { get; set; }
        }
        }

有什么想法吗?自星期五以来,我一直在努力奋斗.如果您知道我可以追踪其想法的方式,那也将有所帮助.

Any ideas? I've been banging my head on this one since Friday. If you know of a way I can track down what it's thinking, that would help, too.

非常感谢.

更多信息:

仅对Id属性的映射进行注释就可以创建模型.但是,这实际上并不能长期有效.

Commenting out the mapping for the Id property alone will allow the model to be created. That won't actually help long-term, though.

推荐答案

该错误是EF的问题.

The error is a problem with EF.

在映射中,我映射Property(p=>p.Id).HasColumnName("MyId");

在从我的Entity类继承的Contact类中,也有ID.

In my Contact class, which inherits from my Entity class, there are also IDs.

因为我没有显式地映射这些列,所以映射被欺骗了",并试图以相同的方式映射我的Contact.Id属性.

Because I did not explicitly map these columns, the mapping "trickled down" and was trying to map my Contact.Id properties in the same fashion.

解决方案:

a)明确映射它们

b)删除继承,以使Contact不是Entity类型,而是ComplexType(没有ID.)

b) Remove the inheritance so that Contact isn't of type Entity and is instead a ComplexType (doesn't have an ID.)

这篇关于错误0019:类型中的每个属性名称在ID字段上必须唯一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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