2014-01-31 17 views
0

我买设计下面的模式:实体框架定义在DB的上下文表

public class LogModel 
    { 
     public class UserActivityLogs 
     { 
      [Key] 
      public int id { get; set; } 

      //Id of the user 
      public string userId { get; set; } 

      //Time of the log 
      public DateTime time { get; set; } 

      public LogActions action { get; set; } 
     } 

     // Types of actions to log 
     public class LogActions 
     { 
      [Key] 
      public int id { get; set; } 

      public string name { get; set; } 

      public string description { get; set; } 
     }  
    } 

现在我想知道什么是我需要在上下文中添加表Logactions以及UserActivityLogs或EF会看到这两个表已链接并自动创建log action表?

也有我指定我的关系正确吗?我的目标是我可以定义多种类型的Logaction,然后用户日志将有一个与之关联的单个日志操作。

回答

0

首先,不要使用嵌套类,这是一个不必要的复杂。使用名称空间来组织类。

其次,不要使用复数名称的类。类的一个实例表示一个实体。此外,使用CamelCase名称作为属性。

第三,是的,实体框架将知道这两个类之间的关联,并创建一个包含两个表和一个外键的数据库模型。

因此,这让你:

namespace MyApp.LogModel 
{ 
    public class UserActivityLog 
    { 
     [Key] 
     public int Id { get; set; } 
     public string UserId { get; set; } 
     public DateTime Time { get; set; } 
     public LogAction LogAction { get; set; } 
    } 

    public class LogAction 
    { 
     [Key] 
     public int Id { get; set; } 
     public string Name { get; set; } 
     public string Description { get; set; } 
    }  
}