定义外键的实体框架
本文关键字:实体 框架 定义 | 更新日期: 2023-09-27 18:06:09
是否有一种方法来分配一个外键表使用实体框架?我使用数据库优先(使用EDMX模型文件)的方法有以下表规范:
表1
classDate
classDate_Id(pk)
classDate_reference
classDate_start
class_Id
表2:
class_Id(pk)
class_Name
classs_description
不可能在创建表(classdate)时指定class_id作为外键。实体表(classdate)本身以class_Id作为列(正常)而不是外键。我想把这个指定为外键。所以. .是否可以指定为外键
这是我在tsgentitesmodel.designer.cs
找到的代码。我需要在这里换车吗?
public partial class tsgEntities : ObjectContext
{
/// <summary>
/// No Metadata Documentation available.
/// </summary>
public ObjectSet<classdate> classdates
{
get
{
if ((_classdates == null))
{
_classdates = base.CreateObjectSet<classdate>("classdates");
}
return _classdates;
}
}
private ObjectSet<classdate> _classdates;
/// <summary>
/// No Metadata Documentation available.
/// </summary>
public ObjectSet<@class> classes
{
get
{
if ((_classes == null))
{
_classes = base.CreateObjectSet<@class>("classes");
}
return _classes;
}
}
private ObjectSet<@class> _classes;
}
/// <summary>
/// Deprecated Method for adding a new object to the classdates EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead.
/// </summary>
public void AddToclassdates(classdate classdate)
{
base.AddObject("classdates", classdate);
}
/// <summary>
/// Deprecated Method for adding a new object to the classes EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead.
/// </summary>
public void AddToclasses(@class @class)
{
base.AddObject("classes", @class);
}
我已经找到了下面的代码,我需要改变这里
public partial class classdate : EntityObject
{
#region Factory Method
/// <summary>
/// Create a new classdate object.
/// </summary>
/// <param name="classDate_End">Initial value of the classDate_End property.</param>
/// <param name="classDate_Id">Initial value of the classDate_Id property.</param>
/// <param name="classDate_Reference">Initial value of the classDate_Reference property.</param>
/// <param name="classDate_Start">Initial value of the classDate_Start property.</param>
/// <param name="class_Id">Initial value of the class_Id property.</param>
public static classdate Createclassdate(global::System.String classDate_End, global::System.Int32 classDate_Id, global::System.String classDate_Reference, global::System.String classDate_Start, global::System.Int32 class_Id)
{
classdate classdate = new classdate();
classdate.classDate_End = classDate_End;
classdate.classDate_Id = classDate_Id;
classdate.classDate_Reference = classDate_Reference;
classdate.classDate_Start = classDate_Start;
classdate.class_Id = class_Id;
return classdate;
}
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.Int32 class_Id
{
get
{
return _class_Id;
}
set
{
Onclass_IdChanging(value);
ReportPropertyChanging("class_Id");
_class_Id = StructuralObject.SetValidValue(value);
ReportPropertyChanged("class_Id");
Onclass_IdChanged();
}
}
private global::System.Int32 _class_Id;
partial void Onclass_IdChanging(global::System.Int32 value);
partial void Onclass_IdChanged();
// same like this for all columns in calss data table
下面是类实体的代码:
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[EdmEntityTypeAttribute(NamespaceName="tsgEntities", Name="class")]
[Serializable()]
[DataContractAttribute(IsReference=true)]
public partial class @class : EntityObject
{
#region Factory Method
/// <summary>
/// Create a new class object.
/// </summary>
/// <param name="class_Description">Initial value of the class_Description property.</param>
/// <param name="class_Id">Initial value of the class_Id property.</param>
/// <param name="class_Name">Initial value of the class_Name property.</param>
/// <param name="class_NumSessions">Initial value of the class_NumSessions property.</param>
/// <param name="class_Reference">Initial value of the class_Reference property.</param>
public static @class Createclass(global::System.String class_Description, global::System.Int32 class_Id, global::System.String class_Name, global::System.String class_NumSessions, global::System.String class_Reference)
{
@class @class = new @class();
@class.class_Description = class_Description;
@class.class_Id = class_Id;
@class.class_Name = class_Name;
@class.class_NumSessions = class_NumSessions;
@class.class_Reference = class_Reference;
return @class;
}
#endregion
#region Primitive Properties
/// <summary>
/// No Metadata Documentation available.
/// </summary>
[EdmScalarPropertyAttribute(EntityKeyProperty=false, IsNullable=false)]
[DataMemberAttribute()]
public global::System.String class_Description
{
get
{
return _class_Description;
}
set
{
Onclass_DescriptionChanging(value);
ReportPropertyChanging("class_Description");
_class_Description = StructuralObject.SetValidValue(value, false);
ReportPropertyChanged("class_Description");
Onclass_DescriptionChanged();
}
}
private global::System.String _class_Description;
partial void Onclass_DescriptionChanging(global::System.String value);
partial void Onclass_DescriptionChanged();
//same like this for all coulmns
如果您想在class_Id
上创建Table2
和Table1
之间的关系,该列需要是Table1
的主键-或者您需要在其上放置一个UNIQUE INDEX:
CREATE UNIQUE INDEX UIX01_ClassDates ON dbo.ClassDates(class_Id)
然后你应该能够创建两个表之间的外键关系:
ALTER TABLE dbo.Classes
ADD CONSTRAINT fk_classes_classdates
FOREIGN KEY (Class_Id) REFERENCES dbo.ClassDates(class_Id)
现在,一旦您完成了这些,刷新您的EDMX模型文件,该关系现在应该会自动显示!(或者您可以手动添加两个类之间的关联,使其显示)