ایجاد ردیف خالی در جدول Parent بعد از SaveChanges
سلاممن یک کلاس از جدول Child میسازم و به db اضافه میکنم و پس از اجرای دستور SaveChanges یک ردیف با اطلاعات Null در جدول Parent ایجاد میشه. چرا این اتفاق میوفته؟ در ستون جدول Child هیچ ارجاعی یه جدول والد نیست و حتی مقدار کلید خارجی در جدول Child رو یک عدد ثابت میذارم که معادل یک ردیف در جدول Parent هست باز هم این اتفاق میوفته. از EF استفاده میکنم. لطفا راهنمایی کنید.تشکر
1 ضمیمه
نقل قول: ایجاد ردیف خالی در جدول Parent بعد از SaveChanges
ضمیمه 153738
سلام
خروجی دستورات مطابق تصویر بالا خواهد بود.
جدول سمت راست فهرست کلاس های مورد نظری هستند که در آنها متد تعریف کردم.
جدول سمت چپ باید متدهای مربوط به هر یک از کلاس هایی که در جدول سمت راست هستند را نگهداری کنه.
بخش اول دستورات جهت ثبت لیست کلاس ها:
foreach (Type t in controllerTypes)
{
tblPermissionController controllerData = new tblPermissionController();
controllerData.fldNamespace = t.Namespace;
controllerData.fldName = t.Name.Replace("Controller", "");
var controllerAttributes = t.CustomAttributes;
if (controllerAttributes != null)
{
foreach (CustomAttributeData att in controllerAttributes)
{
controllerData.fldTitle = att.AttributeType == typeof(cesTitleAttribute) ? att.ConstructorArguments[0].ToString().Replace(""", string.Empty) : string.Empty;
controllerData.fldAuthorized = att.AttributeType == typeof(AuthorizeAttribute) ? true : false;
controllerData.fldRoutPrefix = att.AttributeType == typeof(RoutePrefixAttribute) ? att.ConstructorArguments[0].ToString() : string.Empty;
controllerData.fldDefaultAction = att.AttributeType == typeof(RouteAttribute) ? att.ConstructorArguments[0].Value.ToString() : string.Empty;
controllerData.fldRoutArea = att.AttributeType == typeof(RouteAreaAttribute) ? att.ConstructorArguments[0].Value.ToString() : string.Empty;
}
}
_ControllerList.Add(controllerData);
tblPermissionController IsControllerExist =
db.tblPermissionController.Where(c => c.fldNamespace == controllerData.fldNamespace && c.fldName == controllerData.fldName).FirstOrDefault();
if (IsControllerExist == null)
{
try
{
db.tblPermissionController.Add(controllerData);
db.SaveChanges();
}
catch (Exception)
{
throw;
}
}
else
{
controllerData.fldId = IsControllerExist.fldId;
}
ادامه دارد...
تا ایجا همه چیز خوب پیش میره و لیست کلاس ها ثبت میشه. کدهای بعدی که در ادامه کدهای بالا می باشد باید لیست متدهای موجود در کلاس را ذخیره کند:
//ایجاد لیست متدهای موجود در کنترلر جاری
List<MethodInfo> methods = t.GetMethods().ToList();
foreach (MethodInfo m in methods)
{
//فقط متدهایی که صفت سفارشی را داشته باشند در برنامه ثبت میشوند
if (!m.CustomAttributes.Any(a => a.AttributeType == typeof(cesTitleAttribute)))
{
continue;
}
tblPermissionAction actionData = new tblPermissionAction();
actionData.fldControllerId = controllerData.fldId;
actionData.fldName = m.Name;
var actionAttributes = m.CustomAttributes;
if (actionAttributes != null)
{
foreach (CustomAttributeData att in actionAttributes)
{
//data.fldTitle =att.ConstructorArguments[0].ToString().Replace("","").Replace(""","");
actionData.fldTitle = att.AttributeType == typeof(cesTitleAttribute) ? att.ConstructorArguments[0].ToString().Replace(""", string.Empty) : string.Empty;
actionData.fldAuthorized = att.AttributeType == typeof(AuthorizeAttribute) ? true : false;
actionData.fldAnonumous = att.AttributeType == typeof(AllowAnonymousAttribute) ? true : false;
actionData.fldChildActionOnly = att.AttributeType == typeof(ChildActionOnlyAttribute) ? true : false;
actionData.fldHttpPost = att.AttributeType == typeof(HttpPostAttribute) ? true : false;
actionData.fldActionName = att.AttributeType == typeof(ActionNameAttribute) ? att.ConstructorArguments[0].Value.ToString() : string.Empty;
actionData.fldRout = att.AttributeType == typeof(RouteAttribute) ? att.ConstructorArguments[0].ToString() : string.Empty;
actionData.fldRoutName = att.AttributeType == typeof(RouteAttribute) ? att.NamedArguments[0].ToString().Split(Convert.ToChar("="))[1] : string.Empty;
actionData.fldOutputCash = att.AttributeType == typeof(OutputCacheAttribute) ? true : false;
}
}
_ActionList.Add(actionData);
db.tblPermissionAction.Add(actionData);
}
}
db.SaveChanges();
جالبی مساله این هستش که حتی مقدار فیلد fldControllerId که در جدول Actionها هست رو بصورت دستی و معادل مقادیر از پیش ثبت شده در جدول بانک اطلاعاتی قرار میدم بازهم در زمان ثبت اطلاعات متدها، ایتدا یک ردیف خالی در جدول Controller ایجاد میکنه و از شماره ردیف جدید اون برای ثبت اطلاعات در جدول Child استفاده میکنه. اگر راهنمایی کنید ممنون میشم.
تشکر