使用linq

时间:2018-08-02 05:39:26

标签: c# asp.net linq model-view-controller entity

我有一个正在使用的ASP.NET MVC应用程序,用户可以登录/注册为管理员,个人帐户或公司。.公司和个人模型的属性是其他对象的列表。我需要知道如何向个人登录列表中添加一些内容,并在数据库中对其进行更新

我正在控制器中工作的方法。

 public ActionResult Create([Bind(Include = "Id,Name,Date,Location,StartTime,EndTime,IsPublic")] Event @event)
        {
            string currentUserId = User.Identity.GetUserId();
            ApplicationUser currentUser = db.Users.FirstOrDefault(x => x.Id == currentUserId);

            if (ModelState.IsValid)
            {
                db.Events.Add(@event);
                db.SaveChanges();
                if (User.IsInRole("Company"))
                {
                    Company CompanyHosting = db.Companies.FirstOrDefault(x => x.Email == currentUser.Email);
                    var company = from e in db.PersonalUsers
                                 where e.Email == currentUser.Email
                                 select e;
                    foreach (var item in company)
                    {
                        item.HostedEvents.Add(@event);
                        db.SaveChanges();
                    }
                    db.SaveChanges();
                }
                else if (User.IsInRole("PersonalUser"))
                {
                    PersonalUser PersonHosting = db.PersonalUsers.FirstOrDefault(x => x.Email == currentUser.Email);
                    var person = from e in db.PersonalUsers
                                 where e.Email == currentUser.Email
                                 select e;
                    foreach (var item in person)
                    {
                        item.HostedEvents.Add(@event);
                        db.SaveChanges();
                    }
                    db.SaveChanges();
                }
                return RedirectToAction("Index");
            }

            return View(@event);
        }

与用于个人用户的模型。公司看上去几乎相同。

public class PersonalUser
{
    [Key]
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public string PhoneNumber { get; set; }
    public string Password { get; set; }
    public bool AcceptsTextNotifications { get; set; }
    public bool AcceptsEmailNotifications { get; set; }
    public List<PersonalUser> PeopleIFollow = new List<PersonalUser>();
    public List<PersonalUser> PeopleThatFollowMe = new List<PersonalUser>();
    public List<Company> CompaniesIFollow = new List<Company>();
    public List<Company> CompaniesThatFollowMe = new List<Company>();
    public List<Event> HostedEvents = new List<Event>();
    public List<Event> EventsGoingTo = new List<Event>();

    [ForeignKey("ApplicationUser")]
    public string ApplicationUserID { get; set; }
    public ApplicationUser ApplicationUser { get; set; }
}

事件模型。.

   public class Event
    {
        [Key]
        public int Id { get; set; }
        public string Name { get; set; }
        public DateTime Date { get; set; }
        public string Location { get; set; }
        public string StartTime { get; set; }
        public string EndTime { get; set; }
        public bool IsPublic { get; set; }

        public List<PersonalUser> PersonalInvited = new List<PersonalUser>();
    }

为什么行     item.HostedEvents.Add(@event);     db.SaveChanges(); 不要更新数据库,而是将该事件插入“个人”或“公司”模型列表中的HostedEvents中?

1 个答案:

答案 0 :(得分:0)

您只是将事件添加到项目的EventList中,而没有告诉DbContext更新项目。如下更改:

foreach (var item in person)
{
    item.HostedEvents.Add(@event);
    db.PersonalUsers.Update(item);
}
db.SaveChanges();
相关问题