Note that there are some explanatory texts on larger screens.

plurals
  1. POMVC: New row added to Foreign Key table instead of updating related column
    primarykey
    data
    text
    <p>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</p> <pre><code> 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; } } </code></pre> <p>Department</p> <pre><code> public class Department : BaseEntity { public string Code { get; set; } public string DepartmentName { get; set; } public string ShortName { get; set; } } </code></pre> <p>BaseEntity</p> <pre><code> public class BaseEntity { public int Id { get; set; } [ScaffoldColumn(false)] public bool IsDeleted { get; set; } } </code></pre> <p>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.</p> <p>This is my Department dropdown</p> <pre><code>@Html.DropDownListFor(x =&gt; x.Department.Id, Model.Departments, new {@class = "ui-select",@id="ddlDepartments"}) </code></pre> <p>Model.Departments is </p> <pre><code>public IEnumerable&lt;SelectListItem&gt; Departments { get; set; } </code></pre> <p>İ cant understand why it works like that please explain me whats going wrong. İf you need more inforation i will write back. Thank you.</p> <p>Edit:</p> <p>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?</p> <pre><code>public class DataContext : DbContext { public DataContext() : base("DataContext") { } public IDbSet&lt;Announcement&gt; Announcements { set; get; } public IDbSet&lt;Department&gt; Departments { set; get; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { //I dont know what to do here? } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload