[英]Entity Framework, How can I get the actual data of my class to another class
[英]How can I get data from another entity using class in between in Entity Framework
我有一個使用ASP.NET MVC和Entity Framework的項目。 這是我的實體:
public class Event
{
public Event()
{
UserEvent = new HashSet<UserEvent>();
}
public int EventId { get; set; }
public string EventName { get; set; }
public string Description { get; set; }
[DataType(DataType.Date)]
public DateTime StartDate { get; set; }
[DataType(DataType.Date)]
public DateTime EndDate { get; set; }
public string Time { get; set; }
public int NumberOfVolunteer { get; set; }
public virtual ICollection<UserEvent> UserEvent { get; private set; }
}
這是我在兩個實體中間的桌子
public class UserEvent
{
public int UserId { get; set; }
public int EventId { get; set; }
public string Type { get; set; }
public virtual Event Event { get; set; }
public virtual User User { get; set; }
}
這是我的用戶表。
public class User
{
public User()
{
this.UserEvent = new HashSet<UserEvent>();
}
public int UserId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string UserName { get; set; }
public string Email { get; set; }
public string Password { get; set; }
public Gender? GenderType { get; set; }
public virtual ICollection<UserEvent> UserEvent { get; private set; }
}
這是我控制器中的方法。
public ViewResult Index(string searchString = null, int userId = 0, string ingredients = null)
{
var myEvents = _eventService.GetAllEnum();
if (!String.IsNullOrEmpty(searchString))
{
myEvents = myEvents.Where(r => r.EventName.ToUpper().Contains(searchString.ToUpper()));
}
if (userId > 0)
{
myEvents = myEvents.Where(r => r.userId == userId).ToList();
}
}
}
我的問題是如何使用擴展方法或連接從事件類獲取userId? 我是該領域的新手,我不知道要獲取它,因為我想根據用戶過濾Event
。
我添加中間表的想法是為了防止多對多關系,並且我還創建了自定義映射
注意:我已經創建了一個通用存儲庫
這是我的DbContext
public class MyFinalOneTwoHandsContext :DbContext
{
public MyFinalOneTwoHandsContext()
:base("Name=MyFinalOneTwoHandsContext")
{
}
public DbSet<User> Users { get; set; }
public DbSet<Event> Events { get; set; }
public DbSet<UserEvent> UserEvents { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new UserMap());
modelBuilder.Configurations.Add(new EventMap());
modelBuilder.Configurations.Add(new UserEventMap());
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.