1

使用 Asp.net MVC 4 和 EF 5.0

问题:

每当我在下拉列表中选择一个用户并将帖子提交到操作时。发生两件事:

  1. 用户被添加到 DinnerEvent 应该是:

  2. 选择的用户是重复的,因此我现在在数据库的用户表中有两个用户实例。哪个不好。:(




这是设置

我创建了两个实体: DinnerEvent 和 User

DinnerEvent 类具有指向Attendants(用户)集合的导航属性

public virtual ICollection<User> Attendants { get; set; }

我为 DinnerEvent 和 Users 创建了存储库

DinnerEvent 控制器中的AddAttendantToEvent操作

[HttpPost]
public ActionResult AddAttendantToEvent(int users  , int EventID )
{
    if (ModelState.IsValid)
    {
        var user = userRepository.Find(users);
        dinnereventRepository.Find(EventID).Attendants.Add(user); //Add the User to the Event.
        dinnereventRepository.Save();
        return RedirectToAction("Index");
    }
    else
    {
        return View();
    }
}

视图- 遍历所有事件,并添加一个下拉列表,其中填充了所有用户 foreach 事件。

 @model Madklub.Models.ViewModel

 @foreach (var item in Model.events) {

 //Code to display available events

     @using (Html.BeginForm("AddAttendantToEvent", "DinnerEvents")) {

     @Html.Hidden("EventID", item.DinnerEventID);
     @Html.DropDownListFor(m => m.users, new SelectList(Model.users, "UserID", "RoomNumber"));

     <input type="submit" value="Add" />
}

ViewModel:

public class ViewModel
{
    public IQueryable<DinnerEvent> events { get; set; }
    public IQueryable<User> users { get; set; }
}

which is initialized like this:

Index Action in DinnerEventController:

public ViewResult Index()
{
    ViewModel viewdata = new ViewModel();
    viewdata.events = dinnereventRepository.AllIncluding(m => m.Attendants);
    viewdata.users = userRepository.All;
    return View(viewdata);
}

What am I doing wrong?

请求的存储库代码:

缩小为仅包含 Save() 方法。

请注意,这都是 Scaffolding 自动生成的代码。

public class DinnerEventRepository : IDinnerEventRepository
{
    MadklubContext context = new MadklubContext();

    public void Save()
    {
        context.SaveChanges();
    }        
}

public class MadklubContext : DbContext
 {     

   public DbSet<Madklub.Models.User> Users { get; set; }

   public DbSet<Madklub.Models.MadklubEvent> MadklubEvents { get; set; }

 }
4

1 回答 1

2

工作单元模式:(顺便说一句,使您的回购/uow IDisposable 以便您可以清理 DbContext 实例)

public class DinnerEventRepository : IDinnerEventRepository
{
    MadklubContext _context = new MadklubContext();

    public void Save()
    {
        _context.SaveChanges();
    }  

    public DinnerEventRepository( MadklubContext context = null )
    {
        _context = context ?? new MadklubContext();
    }      
}

public class UserRepository //: IUserRepository
{
    MadklubContext _context = new MadklubContext();

    public void Save()
    {
        _context.SaveChanges();
    }  

    public UserRepository( MadklubContext context = null )
    {
        _context = context ?? new MadklubContext();
    }      
}

public class RsvpUnitOfWork // come up with a better name
{
    MadklubContext _context = new MadklubContext();

    public DinnerEventRepository DinnerEventRepo { get; private set; }
    public UserRepository UserRepo { get; private set; }

    public RsvpUnitOfWork()
    {
        DinnerEventRepo = new DinnerEventRepository( _context );
        UserRepo = new UserRepository( _context );
    }

    public void Save()
    {
        _context.SaveChanges();
    }
}

用法:

[HttpPost]
public ActionResult AddAttendantToEvent(int users  , int EventID )
{
    if (ModelState.IsValid)
    {
        // this should be a using statement once you implement IDisposable
        // e.g. using( var uow = new RsvpUnitOfWork() ) { ... }
        var uow = new RsvpUnitOfWork();
        // you need to validate that user != null
        var user = uow.UserRepo.Find(users);
        // possible null reference exception if dinner event not found for EventID
        uow.DinnerEventRepo.Find(EventID).Attendants.Add(user); //Add the User to the Event.
        uow.Save();
        return RedirectToAction("Index");
    }
    else
    {
        return View();
    }
}
于 2013-10-04T20:43:18.753 回答