MVC实体框架映射正确但表不正确 [英] MVC Entity Framework Mapping Correct but Tables incorrect

查看:142
本文介绍了MVC实体框架映射正确但表不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MVC4实体框架制作票务系统。



3个课程之间的关系
用户到Admin = 1:0 ... 1 1到0或1 =很少的用户将具有管理状态,但不是全部。



管理员Ticket = 1:* | 1 to Many =管理员可以分配超过1张票,但一张票只能分配给一个管理员



用户到Ticket = 1:* | 1 to Many =用户可以打开多张票,但一张票只能有一个用户。



这里我的问题是为什么我的管理表显示错误地看到 HERE



我希望通过TicketID识别每张票,所以在管理表中应该有AdminID(要解决问题的管理员),UserID(提交机票的人)和TicketID。



我遵循一个教程,显示1到多个关系,其中Team = Admin(1)给玩家=门票(许多)



Player.cs(Tickets)

  public class Player 
{
public int PlayerId {得到;组; }
public string Name {get;组; }
public int TeamId {get;组; }

public virtual Team Team {get;组; } //这是新的
}

Team.cs(Admin)

  public class Team 
{
public int TeamId {get;组; }
[必需] public string Name {get;组; }
public string City {get;组; }
public DateTime Founded {get;组; }

public virtual ICollection< player>玩家{get;组; } //这是新的
}

我的代码。



User.cs

  public class User 
{
[Key]
public int UserID {get;组; }
[StringLength(50,MinimumLength = 1)]
public string LastName {get;组; }
[StringLength(50,MinimumLength = 1,ErrorMessage =名字不能超过50个字符。)]

[列(FirstName)]
public string FirstMidName {get;组; }

[DataType(DataType.Date)]
[DisplayFormat(DataFormatString ={0:yyyy-MM-dd},ApplyFormatInEditMode = true)]
public DateTime注册日期{get;组; }

public string FullName
{
get {return LastName +,+ FirstMidName; }
}
// public int AdminiID {get;组; }
[ForeignKey(AdminID)]
public virtual Administrator Administrator {get;组; }

public string AdminRole {get;组; }


public int DepartmentID {get;组; }
[ForeignKey(DepartmentID)]
public virtual Department Department {get;组; }


public int DepotID {get;组; }
[ForeignKey(DepotID)]
public virtual Depot Depot {get;组; }

// public int TicketID {get;组; }
public virtual ICollection< Ticket>门票{get;组;

}

Ticket.cs

  public class Ticket 
{
public int TicketID {get;组; }
public string Issue {get;组; }
[DisplayFormat(NullDisplayText =No Priority)]
public Priority?优先级{get;组; }

[ForeignKey(CategoryID)]
public virtual Category Category {get;组; }
public int CategoryID {get;组; }
public int AdminID {get;组; }
// [ForeignKey(AdminID)]
// public Administrator Admin {get;组; }

[ForeignKey(UserID)]
public virtual User User {get;组; }
public int UserID {get;组; }
}

Administor.cs

  public class Administrator 
{
[ForeignKey(User)]
public int UserID {get;组;
[Key]
public int AdminID {get;组; }
public int TicketID {get;组; }
[StringLength(50)]

public virtual ICollection< Ticket>门票{get;组; }
public virtual User User {get;组; }
}

Configuration.cs

 命名空间RecreationServicesTicketingSystem.Migrations 
{
using System;
使用System.Data.Entity;
使用System.Data.Entity.Migrations;
使用System.Linq;
使用RecreationalServicesTicketingSystem.Models;
使用System.Collections.Generic;

内部密封类配置:DbMigrationsConfiguration< RecreationalServicesTicketingSystem.DAL.IssueContext>
{
public Configuration()
{
AutomaticMigrationsEnabled = false;
}

protected override void Seed(RecreationalServicesTicketingSystem.DAL.IssueContext context)
{
var departments = new List< Department>
{
new Department {DepartmentID = 1,Name =IT},
new Department {DepartmentID = 2,Name =Admin},
new Department {DepartmentID = 3,Name =Human Resources},
new Department {DepartmentID = 4,Name =Mechanics},
new Department {DepartmentID = 5,Name =Director},
新部门{DepartmentID = 6,Name =Operations}

};
departments.ForEach(s => context.Departments.AddOrUpdate(p => p.Name,s));
context.SaveChanges();


var depots = new List< Depot>
{
new Depot {DepotID = 1,Name =Porana},
new Depot {DepotID = 2,Name =Far North},


};
depots.ForEach(s => context.Depots.AddOrUpdate(p => p.Name,s));
context.SaveChanges();

var users = new List< User>
{
new User {FirstMidName =Jason,LastName =Wan,
注册日期= DateTime.Parse(20162-18),DepartmentID = 1,DepotID = 1 ,AdminRole =Administrator LVL1},
new User {FirstMidName =Andy,LastName =Domagas,
注册日期= DateTime.Parse(2016-02-18),DepartmentID = 1 ,DepotID = 1,AdminRole =Administrator LVL2},
new User {FirstMidName =Denis,LastName =Djohar,
EnrollmentDate = DateTime.Parse(2016-02-18) ,DepartmentID = 1,DepotID = 1,AdminRole =Administrator LVL3},
new User {FirstMidName =Christine,LastName =West,
EnrollmentDate = DateTime.Parse -18),DepartmentID = 1,DepotID = 1},

};


users.ForEach(s => context.Users.AddOrUpdate(p => p.FirstMidName,s));
context.SaveChanges();

users.ForEach(s => context.Users.AddOrUpdate(p => p.LastName,s));
context.SaveChanges();


var administrator = new List< Administrator>
{
new Administrator {AdminID = 1,User = users.Single(s => s.UserID == 1),
/ * Tickets = new List< Ticket>()* /},
new Administrator {AdminID = 2,User = users.Single(s => s.UserID == 2),
/ * Tickets = new List< Ticket>()* /} ,
new Administrator {AdminID = 3,User = users.Single(s => s.UserID == 3),
/ * Tickets = new List< Ticket>()* /}

};
administrator.ForEach(s => context.Administrators.AddOrUpdate(p => p.AdminID,s));
context.SaveChanges();

var categories = new List< Category>
{
new Category {CategoryID = 0001,Title =Desktop},
new Category {CategoryID = 0002,Title =Mobile},
new Category {CategoryID = 0003,Title =Menzits},
new Category {CategoryID = 0004,Title =XMPRO},
new Category {CategoryID = 0005,Title =Con-X},
new Category {CategoryID = 0006,Title =Promapp},
new Category {CategoryID = 0007,Title =QGIS},
};
categories.ForEach(s => context.Categories.AddOrUpdate(p => p.Title,s));
context.SaveChanges();

var tickets = new List< Ticket>
{
new Ticket {
UserID = users.Single(s => s.LastName ==Wan)。UserID,
CategoryID = categories.Single(c =   c.Title ==Con-X).CategoryID,
Issue =(Con-X Login Error),
AdminID = 1,
Priority = Priority.High
},
new Ticket {
UserID = users.Single(s => s.LastName ==Wan)。UserID,
CategoryID = categories.Single(c = > c.Title ==Desktop).CategoryID,
Issue =(Can not remote access C0123),
AdminID = 2,
Priority = Priority.Med
},
};


foreach(门票中的票e)
{
var ticketInDataBase = context.Tickets.Where(
s =>
s。 User.UserID == e.UserID&&
s.Category.CategoryID == e.CategoryID).SingleOrDefault();
if(ticketInDataBase == null)
{
context.Tickets.Add(e);
}
}
context.SaveChanges();
}
}
}


解决方案

一切都好起来如果您有多个到管理员到门票,有一些(许多)门票属于一个管理员,所以他们应该有一个AdminID列。并且在这个关系的管理表上没有列。这就是它的工作原理


I'm currently making a ticketing system using MVC4 Entity Framework.

Relations between 3 classes User to Admin = 1:0...1 1 to 0 or 1 = Few users will have admin status but not all.

Admin to Ticket = 1:*|1 to Many = Admins can be assigned with more than 1 ticket but one ticket can only be assigned to one admin

User to Ticket = 1:*|1 to Many = Users can open multiple tickets but one ticket can only have one user.

Here are what my current tables look like.

My issue is why is my admin table displaying incorrectly see HERE

I want each ticket to be identified by a TicketID so on the Admin table it should have the AdminID(Admin that's going to fix the issue),UserID(The person who submitted the ticket) and TicketID.

I followed a tutorial that shows 1 to many relationships where Team = Admin(1) to Players = Tickets(Many)

Player.cs ( Tickets)

public class Player
{
    public int PlayerId { get; set; }
    public string Name { get; set; }
    public int TeamId { get; set; }

    public virtual Team Team { get; set; } // This is new
}

Team.cs (Admin)

public class Team
{
    public int TeamId { get; set; }
    [Required] public string Name { get; set; }
    public string City { get; set; }
    public DateTime Founded { get; set; }

    public virtual ICollection<player> Players { get; set; } // This is new
}

My code.

User.cs

    public class User
    {
        [Key]
        public int UserID { get; set; }
        [StringLength(50, MinimumLength = 1)]
        public string LastName { get; set; }
        [StringLength(50, MinimumLength = 1, ErrorMessage = "First name cannot be longer than 50 characters.")]

        [Column("FirstName")]
        public string FirstMidName { get; set; }

        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime EnrollmentDate { get; set; }

        public string FullName
        {
            get { return LastName + ", " + FirstMidName; }
        }
   //     public int AdminiID { get; set; }
        [ForeignKey("AdminID")]
        public virtual Administrator Administrator { get; set; }

        public string AdminRole { get; set; }


        public int DepartmentID { get; set; }
        [ForeignKey("DepartmentID")]
        public virtual Department Department { get; set; }


        public int DepotID { get; set; }
        [ForeignKey("DepotID")]
        public virtual Depot Depot { get; set; }

        //public int TicketID { get; set; }
        public virtual ICollection<Ticket> Tickets { get; set; }

    }

Ticket.cs

public class Ticket
{
    public int TicketID { get; set; }
    public string Issue { get; set; }
    [DisplayFormat(NullDisplayText = "No Priority")]
    public Priority? Priority { get; set; }

    [ForeignKey("CategoryID")]
    public virtual Category Category { get; set; }
    public int CategoryID { get; set; }
    public int AdminID { get; set; }
    //[ForeignKey("AdminID")]
    //public Administrator Admin { get; set; }

    [ForeignKey("UserID")]
    public virtual User User { get; set; }
    public int UserID { get; set; }
}

Administor.cs

public class Administrator
{
    [ForeignKey("User")]
    public int UserID { get; set; }
    [Key]
    public int AdminID { get; set; }
    public int TicketID { get; set; }        
    [StringLength(50)]

    public virtual ICollection<Ticket> Tickets { get; set; }
    public virtual User User { get; set; }
}

Configuration.cs

namespace RecreationServicesTicketingSystem.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;
    using RecreationalServicesTicketingSystem.Models;
    using System.Collections.Generic;

    internal sealed class Configuration : DbMigrationsConfiguration<RecreationalServicesTicketingSystem.DAL.IssueContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(RecreationalServicesTicketingSystem.DAL.IssueContext context)
        {
            var departments = new List<Department>
            {
                new Department { DepartmentID = 1, Name = "IT"},
                new Department { DepartmentID = 2, Name = "Admin" },
                new Department { DepartmentID = 3, Name = "Human Resources"},
                new Department { DepartmentID = 4, Name = "Mechanics" },
                new Department { DepartmentID = 5, Name = "Directors" },
                new Department { DepartmentID = 6, Name = "Operations"}

            };
            departments.ForEach(s => context.Departments.AddOrUpdate(p => p.Name, s));
            context.SaveChanges();


            var depots = new List<Depot>
            {
                new Depot { DepotID = 1, Name = "Porana"},
                new Depot { DepotID = 2, Name = "Far North"},


            };
            depots.ForEach(s => context.Depots.AddOrUpdate(p => p.Name, s));
            context.SaveChanges();

            var users = new List<User>
        {
            new User { FirstMidName = "Jason",   LastName = "Wan",
                EnrollmentDate = DateTime.Parse("2016-02-18"), DepartmentID = 1, DepotID = 1,AdminRole="Administrator LVL1"},
            new User { FirstMidName = "Andy", LastName = "Domagas",
                EnrollmentDate = DateTime.Parse("2016-02-18"), DepartmentID = 1,DepotID = 1,AdminRole="Administrator LVL2"},
            new User { FirstMidName = "Denis",   LastName = "Djohar",
                EnrollmentDate = DateTime.Parse("2016-02-18"), DepartmentID = 1 ,DepotID = 1,AdminRole="Administrator LVL3"},
            new User { FirstMidName = "Christine",   LastName = "West",
                EnrollmentDate = DateTime.Parse("2016-02-18"), DepartmentID = 1, DepotID = 1},

        };


            users.ForEach(s => context.Users.AddOrUpdate(p => p.FirstMidName, s));
            context.SaveChanges();

            users.ForEach(s => context.Users.AddOrUpdate(p => p.LastName, s));
            context.SaveChanges();


            var administrator = new List<Administrator>
            {
                new Administrator {AdminID = 1,  User = users.Single ( s => s.UserID == 1),
                /*Tickets = new List<Ticket>()*/ },
                new Administrator {AdminID = 2, User = users.Single ( s => s.UserID == 2),
                /*Tickets = new List<Ticket>()*/ },
                new Administrator {AdminID = 3,  User = users.Single ( s => s.UserID == 3),
                /*Tickets = new List<Ticket>() */}

            };
            administrator.ForEach(s => context.Administrators.AddOrUpdate(p => p.AdminID, s));
            context.SaveChanges();

            var categories = new List<Category>
            {
                new Category {CategoryID = 0001, Title = "Desktop"},
                new Category {CategoryID = 0002, Title = "Mobile"},
                new Category {CategoryID = 0003, Title = "Menzits"},
                new Category {CategoryID = 0004, Title = "XMPRO"},
                new Category {CategoryID = 0005, Title = "Con-X"},
                new Category {CategoryID = 0006, Title = "Promapp"},
                new Category {CategoryID = 0007, Title = "QGIS"},
            };
            categories.ForEach(s => context.Categories.AddOrUpdate(p => p.Title, s));
            context.SaveChanges();

            var tickets = new List<Ticket>
            {
                new Ticket {
                    UserID = users.Single(s => s.LastName == "Wan").UserID,
                    CategoryID = categories.Single(c => c.Title == "Con-X" ).CategoryID,
                    Issue = ("Con-X Login Error"),
                    AdminID = 1,
                    Priority = Priority.High
                },
                new Ticket {
                    UserID = users.Single(s => s.LastName == "Wan").UserID,
                    CategoryID = categories.Single(c => c.Title == "Desktop" ).CategoryID,
                    Issue = ("Can't remote access C0123"),
                    AdminID = 2,
                    Priority = Priority.Med
                },
            };


            foreach (Ticket e in tickets)
            {
                var ticketInDataBase = context.Tickets.Where(
                    s =>
                        s.User.UserID == e.UserID &&
                        s.Category.CategoryID == e.CategoryID).SingleOrDefault();
                if (ticketInDataBase == null)
                {
                    context.Tickets.Add(e);
                }
            }
            context.SaveChanges();
        }
    }
}

解决方案

Everything look OK. If you have many to one for admins to tickets there are some (many) tickets that belong to one admin so they should have an AdminID column. And no column on the admin table for this relationship. That's how it works!

这篇关于MVC实体框架映射正确但表不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆