Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 实体framewrok如何确定数据库优先方法中的主键_C#_Entity Framework_Ado.net - Fatal编程技术网

C# 实体framewrok如何确定数据库优先方法中的主键

C# 实体framewrok如何确定数据库优先方法中的主键,c#,entity-framework,ado.net,C#,Entity Framework,Ado.net,我的sql server 2008中有以下三个模型类,我使用ado.net实体框架映射了表:- 数据库中ITSwitchPort表的主键是TechnologyID&SwitchID: public partial class ITSwitchPort { public int TechnologyID { get; set; } public int SwitchID { get; set; } public string Port

我的sql server 2008中有以下三个模型类,我使用ado.net实体框架映射了表:-

数据库中ITSwitchPort表的主键是TechnologyID&SwitchID:

public partial class ITSwitchPort

    {

        public int TechnologyID { get; set; }

        public int SwitchID { get; set; }

        public string PortNumber { get; set; }



        public virtual Technology Technology { get; set; }

        public virtual ITSwitch ITSwitch { get; set; }

    }
public partial class ITSwitch

    {

        public ITSwitch()

        {

            this.ITSwitchPorts = new HashSet<ITSwitchPort>();

        }



        public int SwitchID { get; set; }

        public Nullable<int> ModelID { get; set; }

        public string Spec { get; set; }

        public int RackID { get; set; }

        public Nullable<int> ConsoleServerID { get; set; }

        public string Description { get; set; }

        public long IT360SiteID { get; set; }

        public byte[] timestamp { get; set; }

        public string ConsoleServerPort { get; set; }



        public virtual SwitchModel SwitchModel { get; set; }

        public virtual Technology Technology { get; set; }



        public virtual ITRack ITRack { get; set; }

        public virtual ICollection<ITSwitchPort> ITSwitchPorts { get; set; }

    }
数据库中技术表的主键是TechnologyID:

public partial class Technology

    {

        public Technology()

        {

            this.ITServers = new HashSet<ITServer>();

            this.ITSwitchPorts = new HashSet<ITSwitchPort>();

            this.TechnologyAudits = new HashSet<TechnologyAudit>();

            this.TechnologyIPs = new HashSet<TechnologyIP>();

        }



        public int TechnologyID { get; set; }

        public string Tag { get; set; }

        public bool IsDeleted { get; set; }

        public byte[] timestamp { get; set; }

        public Nullable<int> TypeID { get; set; }

        public Nullable<System.DateTime> StartDate { get; set; }

        public Nullable<long> IT360ID { get; set; }

        public bool IsCompleted { get; set; }

        public Nullable<long> PartialTag { get; set; }

        public bool IsManaged { get; set; }



        public virtual ICollection<ITSwitchPort> ITSwitchPorts { get; set; }

    }
公共部分类技术
{
公共技术()
{
this.ITServers=new HashSet();
this.itswitchport=new HashSet();
this.TechnologyAudits=新的HashSet();
this.TechnologyIPs=new HashSet();
}
public int TechnologyID{get;set;}
公共字符串标记{get;set;}
公共布尔被删除{get;set;}
公共字节[]时间戳{get;set;}
公共可为空的TypeID{get;set;}
公共可为空的起始日期{get;set;}
公共可空IT360ID{get;set;}
公共布尔已完成{get;set;}
公共可空的PartialTag{get;set;}
公共布尔值受管理{get;set;}
公共虚拟ICollection ITSwitchPorts{get;set;}
}
数据库中ITSwitch表的主键是SwitchID:

public partial class ITSwitchPort

    {

        public int TechnologyID { get; set; }

        public int SwitchID { get; set; }

        public string PortNumber { get; set; }



        public virtual Technology Technology { get; set; }

        public virtual ITSwitch ITSwitch { get; set; }

    }
public partial class ITSwitch

    {

        public ITSwitch()

        {

            this.ITSwitchPorts = new HashSet<ITSwitchPort>();

        }



        public int SwitchID { get; set; }

        public Nullable<int> ModelID { get; set; }

        public string Spec { get; set; }

        public int RackID { get; set; }

        public Nullable<int> ConsoleServerID { get; set; }

        public string Description { get; set; }

        public long IT360SiteID { get; set; }

        public byte[] timestamp { get; set; }

        public string ConsoleServerPort { get; set; }



        public virtual SwitchModel SwitchModel { get; set; }

        public virtual Technology Technology { get; set; }



        public virtual ITRack ITRack { get; set; }

        public virtual ICollection<ITSwitchPort> ITSwitchPorts { get; set; }

    }
公共部分类ITSwitch
{
公共IT交换机()
{
this.itswitchport=new HashSet();
}
public int SwitchID{get;set;}
公共可为空的ModelID{get;set;}
公共字符串规范{get;set;}
public int RackID{get;set;}
公共可为空的控制台服务器ID{get;set;}
公共字符串说明{get;set;}
公共长IT360SiteID{get;set;}
公共字节[]时间戳{get;set;}
公共字符串控制台服务器端口{get;set;}
公共虚拟SwitchModel SwitchModel{get;set;}
公共虚拟技术技术{get;set;}
公共虚拟ITRack ITRack{get;set;}
公共虚拟ICollection ITSwitchPorts{get;set;}
}

现在,如果我尝试更新ITSwitchPort.SwitchID,我将得到一个错误,即SwitchID是密钥的一部分,无法更新,因此我的问题是实体框架如何确定上述三种模型的密钥?

尽管您的问题已经解决,但您的问题仍然没有得到回答

问题是,实体框架如何知道属性是表主键-DbFirst

答案是,一旦您首先使用数据库,Entity Framework会自动添加实体和表之间的映射,其中一个映射如下:

this.HasKey(a => a.TechnologyID );
在技术映射类中。(这是自动生成的)


通过这段代码,实体Framwork知道哪个属性是主键。

错误告诉您不能更改数据库中PK字段的值。您不应该更改键值,而是用新的记录替换
ITSwitchPort
记录。因此,您的意思是错误来自数据库而不是实体框架,一般来说,实体框架如何确定我的实体的PK?它是否会使用任何名称转换,或者它可以确定来自DB的PK是什么,而不考虑密钥名称?是的,它基于名称约定。EF发现,
ITSwitchPort
技术
ITSwitch
之间的连接表。错误来自EF,但如果您直接在数据库中尝试此操作,您会得到类似的错误。因此,如果我对主键使用不同的名称,我有一个名为PC的表,其中主键为SerialKey,那么这是否意味着当我使用EF映射此表时,它将无法知道密钥?因此,您的意思是它从数据库中读取主键,而不管表名和主键名如何?在我的情况下,我找不到任何tis.HasKey?您能提供一些建议吗?因为您使用的是ADO.NET,只需查看EF创建的生成的.edmx文件,您就会完全理解它。(本例中的映射存在)但我的问题是如何在edmx中进行映射?EF是否将模型的主键设置为与数据库中定义的完全相同,或者将使用特定的条件。例如,将第一个字段设置为PK或将TableName+ID字段设置为主键?因此,我告诉您查看edmx文件,您将在那里找到答案。您将看到EF在数据库中的主键(在数据库中具有相同的名称)与实体中的主键(可能也具有相同的名称)之间映射,表名和实体名之间的名称通常不同(复数)。要查看XML文件:右键单击edmx文件->打开方式->XML编辑器,您将看到如下内容: