简体   繁体   中英

How can I get data from another entity using class in between in Entity Framework

I have an project using ASP.NET MVC with Entity Framework. Here is my entity:

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

Here is my table in middle between two entities

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

Here is my user table.

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

Here is my method in my controller.

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

My question is how to get userId from event class using extension method or join? I am new to this field and I do not know to get it because I want to filter Event based on user.

My idea of adding middle table is to prevent many to many relationship and also I crated custom mapping

Note: I have created a generic repository

This is my 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());
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM