开发者

Entity Framework defining the foreign key

Is there a way to assign a foreign key to the table using Entity Framework? I have following table specification using database-first (with an EDMX model file) approach:

Table 1:

classDate   
classDate_Id(pk)
classDate_reference
classDate_start
class_Id

Table 2:

class_Id(pk)
class_Name
classs_description

It is not possible to specify the class_id as a foreign key at the time of table creation (classdate). The entity table (classdate) itself with class_Id as a column (normal) not as a foreign key. I want to specify this one as foreign key. so.. is it possible to specify as a foreign key.

This is the code I found in tsgentitesmodel.designer.cs. Do I need to change here?

  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);
    }

And I have found the below code and do I need to change here

 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 

Below is the code for class entity:

  /// <summary>
/// No Metadata Documentation available.
/// </summary>
[EdmEntityTypeAttribute(NamespaceName="tsgEntities", Name="class")]
[Serializable开发者_StackOverflow中文版()]
[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 


If you want to create a relationship between Table2 and Table1 on class_Id, that column needs to be the primary key in Table1 - or you need to put a UNIQUE INDEX on it:

CREATE UNIQUE INDEX UIX01_ClassDates ON dbo.ClassDates(class_Id)

And then you should be able to create the foreign key relationship between your two tables:

ALTER TABLE dbo.Classes
ADD CONSTRAINT fk_classes_classdates
FOREIGN KEY (Class_Id) REFERENCES dbo.ClassDates(class_Id)

Now, once you've done that, refresh your EDMX model file and that relationship ought to show up automagically, now! (or you can manually add an Association between your two classes to make it show up)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜