实体框架定义外键

时间:2011-08-21 07:24:01

标签: c# linq entity-framework entity-framework-4

有没有办法使用Entity Framework为表分配外键?我使用数据库优先(使用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&lt;T&gt; 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&lt;T&gt; 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 

1 个答案:

答案 0 :(得分:2)

如果您想在Table2上的Table1class_Id之间建立关系,那么该列必须是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)

现在,一旦你完成了这个,刷新你的E​​DMX模型文件,这个关系应该自动出现,现在! (或者您可以在两个类之间手动添加关联以使其显示)