MVC:新行添加到外键表而不是更新相关列 [英] MVC: New row added to Foreign Key table instead of updating related column

查看:154
本文介绍了MVC:新行添加到外键表而不是更新相关列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是MVC中的新手。我有关于外键列的问题。我有两张叫做Annoucement and Departments的表。
Department_Id是Annoucement表中的外键。我也检查了它的数据库图。这是我的模型

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

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

公共部门{set;得到;
}

部门

  public class Department:BaseEntity 
{

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

BaseEntity

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

我的Annoucement View中有部门下拉菜单,我从中选择一个部门,然后我保存它。
但是在我的Annoucement表中,foreignKey部分填写了插入Departments表的新部门ID。
当我保存它时,它插入新行到Departments表,并将该id插入在department_id列中的Annoucement表。



这是我的部门下拉列表

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

Model.Departments是

  public IEnumerable< SelectListItem>部门{get;组; } 

不明白为什么它的工作原理,请解释我出了什么问题。
如果你需要更多的信息,我会写回来。
谢谢。



编辑:



我在搜索时找到一些东西。我想我在上下文类中做某事。我应该添加OnModelCreating,但我不知道该怎么做?

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

}

public IDbSet< Announcement>公告{set;得到; }
public IDbSet< Department>部门{set;得到;

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//我不知道该怎么办?
}
}


解决方案

你可以在OnModelCreating中为数据库上下文声明额外的规则和行为,以确保数据库的完整性。一些例子如下:

  modelBuilder.Conventions.Remove< OneToManyCascadeDeleteConvention>(); 

此声明表示我不想删除由外键链接的子记录

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

此声明定义MyTable实体的Quantity字段的精度。



基本上,您的帖子没有足够的信息来直接解决您的问题。
但是您可以尝试为dbcontext添加以下约定。

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

此声明允许您防止在您的Annoucement表中自动生成department_id列。



不要忘记在OnModelCreating方法的末尾添加以下行

  base.OnModelCreating (模型构建器); 


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

解决方案

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

这篇关于MVC:新行添加到外键表而不是更新相关列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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