PDA

View Full Version : سوال: کلید خارجی یک جدول از خود جدول



Iran58
سه شنبه 23 آبان 1396, 10:43 صبح
سلام
دوتا کلاس زیر را داریم

public class City
{
public City()
{


}
public int CityId { get; set; }
public string Name { get; set; }
public int CityTypeId { get; set; }
[ForeignKey(nameof(CityTypeId))]
public virtual CityType CityType { get; set; }
public int ParentId { get; set; }
}


public class CityType
{
public CityType()
{


}
public int CityTypeId { get; set; }
public string Name { get; set; }
public virtual ICollection<City> Cities { get; set; }


}

که

public int ParentId { get; set; }

که id خود جدول می باشد
چگونه می توانم تعریف کنم که ارجاع از خود id همین جدول است
برنامه در mvccore است

ژیار رحیمی
سه شنبه 23 آبان 1396, 10:53 صبح
public class City
{

public int CityId { get; set; }
public string Name { get; set; }
public int CityTypeId { get; set; }
[ForeignKey(nameof(CityTypeId))]
public virtual CityType CityType { get; set; }

public int? ParentId { get; set; }
[ForeignKey(nameof(ParentId))]
public virtual City Parent { get; set; }
}

Iran58
پنج شنبه 25 آبان 1396, 17:57 عصر
public class City
{

public int CityId { get; set; }
public string Name { get; set; }
public int CityTypeId { get; set; }
[ForeignKey(nameof(CityTypeId))]
public virtual CityType CityType { get; set; }

public int? ParentId { get; set; }
[ForeignKey(nameof(ParentId))]
public virtual City Parent { get; set; }
}

سلام
من صفحه زیر را صراحی کردم

@model WebApplication2.Models.City


@{
ViewBag.Title = "Create";
}


<h2>Create</h2>


@using (Html.BeginForm())
{
@Html.AntiForgeryToken()

<div class="form-horizontal">
<h4>City</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.Name, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Name, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Name, "", new { @class = "text-danger" })
</div>
</div>


<div class="form-group">
@Html.LabelFor(model => model.CityTypeId, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.CityTypeId, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.CityTypeId, "", new { @class = "text-danger" })
</div>
</div>


<div class="form-group">
@Html.LabelFor(model => model.ParentId, "ParentId", htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">

@Html.DropDownList("ParentId", null, htmlAttributes: new {@class = "form-control"})
@Html.ValidationMessageFor(model => model.ParentId, "", new {@class = "text-danger"})
</div>
</div>


<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Create" class="btn btn-default" />
</div>
</div>
</div>
}


<div>
@Html.ActionLink("Back to List", "Index")
</div>



اما مشکلی که دارم
بار اول مثلا می نویسم ایران درست ثبت می شود و ParentId هم null ثبت میشود
اما برای بار دیگر می خواهم یک کشور دیگر ثبت کنم ParentId را ایران نشان می دهد درصورتیکه من می خواهم بتوانم یک کشور دیگر ثبت کنم که ParentId انهم null باشد
باید چکار کنم
باتشکر

ژیار رحیمی
پنج شنبه 25 آبان 1396, 19:00 عصر
کدهای مربوط به متد Create در کلاس Controller رو بزارید تا راهنمایی لازم انجام شود.

Iran58
پنج شنبه 25 آبان 1396, 19:32 عصر
// GET: Cities/Create
public ActionResult Create()
{
ViewBag.ParentId = new SelectList(db.Cities, "CityId", "Name");
return View();
}


// POST: Cities/Create
// To protect from overposting attacks, please enable the specific properties you want to bind to, for
// more details see https://go.microsoft.com/fwlink/?LinkId=317598.
[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult Create([Bind(Include = "CityId,Name,CityTypeId,ParentId")] City city)
{
if (ModelState.IsValid)
{
db.Cities.Add(city);
db.SaveChanges();
return RedirectToAction("Index");
}


ViewBag.ParentId = new SelectList(db.Cities, "CityId", "Name", city.ParentId);
return View(city);
}

ژیار رحیمی
جمعه 26 آبان 1396, 01:36 صبح
در ef با مقدار دهی مستقیم کلید خارجی ParentId مقدار دهی entity ان انجام نمی شود باید entity آن بر اساس کلید یافته شود و انتساب داده شود.بر اساس ParentId اگر صفر بود مقدار null واگر بزرگتر از صفر بود entity ان را یافته و انتساب داده میشود.

if (ModelState.IsValid)
{
city.Parent=ParentId>0? db.Cities.Find(ParentId):null;
db.Cities.Add(city);
db.SaveChanges();
return RedirectToAction("Index");
}