MVC: New row added to Foreign Key table instead of

2019-09-02 06:55发布

I am newbie in MVC. I have problem about foreign key column. I have two table called Annoucement and Departments. Department_Id is foreign key in Annoucement table. i also checked it database diagram. Here is my models

  public class Announcement : BaseEntity
{
    public Announcement()
    {
        CreatedDatetime = DateTime.Now;
    }

    public String Title { set; get; }  
    public string ContentText { set; get; } 
    [Display(Name = "Date")]
    [DataType(DataType.DateTime), DisplayFormat(DataFormatString = "{0:dd/MM/yyyy}", ApplyFormatInEditMode = true)]
    public DateTime CreatedDatetime { get; set; } 

    public Department Department { set; get; }    
}

Department

    public class Department : BaseEntity
{

    public string Code { get; set; }
    public string DepartmentName { get; set; }
    public string ShortName { get; set; }
}

BaseEntity

    public class BaseEntity
{
    public int Id { get; set; }
    [ScaffoldColumn(false)]
    public bool IsDeleted { get; set; }
}

i have departments dropdown in my Annoucement View i select one department from it and then i save it. But in my Annoucement Table the foreignKey part fill with new department id which is inserted to Departments table. Shortly when i save it, it inserted new row to the Departments table and take that id to insert Annoucement table in department_id column.

This is my Department dropdown

@Html.DropDownListFor(x => x.Department.Id, Model.Departments, new {@class = "ui-select",@id="ddlDepartments"})

Model.Departments is

public IEnumerable<SelectListItem> Departments { get; set; }

İ cant understand why it works like that please explain me whats going wrong. İf you need more inforation i will write back. Thank you.

Edit:

I find something while searching. I guess i do something in context class. I should add OnModelCreating but i dont know what to do in it?

public class DataContext : DbContext
{
    public DataContext()
        : base("DataContext")
    {

    }

    public IDbSet<Announcement> Announcements { set; get; } 
    public IDbSet<Department> Departments { set; get; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
         //I dont know what to do here?
    }
}

1条回答
太酷不给撩
2楼-- · 2019-09-02 07:30

You can declare additional rules and behaviors for your database context in OnModelCreating in order to ensure the integrity of your database. Some examples are stated below:

modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();

This declaration means, "I do not want to delete children records linked by foreign keys"

modelBuilder.Entity<MyTable>().Property(x => x.Quantity).HasPrecision(7, 1);

This declaration defines the precision of the Quantity field of MyTable entity.

Basically, your post has no enough information to solve your issue directly. But you may try to add following convention for your dbcontext.

modelBuilder.Entity<Annoucement>().Property(p => p.department_id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);

This declaration allows you to prevent auto generation of department_id column in your Annoucement table.

Do not forget to add following line at the end of OnModelCreating method

base.OnModelCreating(modelBuilder);
查看更多
登录 后发表回答