外键参考 [英] Foreign key reference

查看:76
本文介绍了外键参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在SQL中有3个表



UserTable



UserId - >主键

BusinessId - >外键

WorkHourId - > ForeignKey

用户名

EmailId



工作时间表



WorkHourId - >主键

UserId

StartTIme

结束时间





BusinessTable



BusinessId --->主键

商业名称

WorkhourId --->外键



我在MVC做项目。我正在使用EntityFramework来保存数据库中的值。我的问题是如何在业务表中获得工作?请帮帮我



我添加我的代码供你参考

  public  ActionResult Create(tblUser tbluser,FormCollection coll)
{

Session [ EmailId] = tbluser.EmailID.Trim();

// var ck1 = Request.Form.GetValues [ck];
// string ck = coll [ck];
// var ck2 = Request.Form.GetValues(Tuesday);
// var ck3 = Request.Form.GetValues(Wednesday);
// var ck4 = Request.Form.GetValues(Thursday);

// var ck1 = coll [Monday];


if (ModelState.IsValid)
{

WorkingHour hour = tbluser.WorkingHour;
tblBusinessCategory cg = new tblBusinessCategory();
使用(MvcApplication1Entities1 context = new MvcApplication1Entities1())
{
tbluser.WorkingHour = null ;
cg.WorkHourId = hour.WorkHourId;
tbluser.WorkingHours.Add(小时);
// hour.BusinessId = cg.BusinessID;

context.tblUsers.Add(tbluser);

context.SaveChanges();


}


EmailManager.SendConfirmationEmailName(tbluser.EmailID,tbluser.FirstName);
@ Response.Redirect( 〜/ Login / BULogin);

}

ViewBag.AddressId = new SelectList(db.tblAddresses, AddressID Address1 ,tbluser.AddressId);
ViewBag.BusinessCategoryId = new SelectList(db.tblBusinessCategories, BusinessID BusinessName,tbluser.BusinessCategoryId);
ViewBag.UserTypeId = new SelectList(db.tblUserTypes, UserTypeID UserType,tbluser.UserTypeId);


return 查看(tbluser);
}

解决方案

首先,你有一个让生活变得更难的冗余约束:



每个企业可以有1个工作小时

每个用户可以有1个生意



这个逻辑,用户已经有1个工作时间,所以不需要user.workhours fk!



我建议你重新掌握你的桌子关系,让生活更轻松

Hi All,
I have 3 tables in SQL

UserTable

UserId --> primary key
BusinessId --> Foreign key
WorkHourId --> ForeignKey
UserName
EmailId

WorkHour Table

WorkHourId --> primary key
UserId
StartTIme
EndTime


BusinessTable

BusinessId ---> Primary Key
BusinessName
WorkhourId ---> Foreign Key

I am doing project in MVC. I am using EntityFramework to save the values in the database. My question is How do i get the workhourid in the business table?. Please help me

I add my code for your reference

public ActionResult Create(tblUser tbluser, FormCollection coll)
      {

          Session["EmailId"] = tbluser.EmailID.Trim();

          //   var ck1 = Request.Form.GetValues["ck"];
          //  string ck = coll["ck"];
          // var ck2 = Request.Form.GetValues("Tuesday");
          //var ck3 = Request.Form.GetValues("Wednesday");
          //var ck4 = Request.Form.GetValues("Thursday");

          //  var ck1 = coll["Monday"];


          if (ModelState.IsValid)
          {

              WorkingHour hour = tbluser.WorkingHour;
              tblBusinessCategory cg = new tblBusinessCategory();
              using (MvcApplication1Entities1 context = new MvcApplication1Entities1())
              {
                  tbluser.WorkingHour = null;
                  cg.WorkHourId = hour.WorkHourId;
                  tbluser.WorkingHours.Add(hour);
                //  hour.BusinessId = cg.BusinessID;

                  context.tblUsers.Add(tbluser);

                  context.SaveChanges();


              }


              EmailManager.SendConfirmationEmailName(tbluser.EmailID, tbluser.FirstName);
              @Response.Redirect("~/Login/BULogin");

          }

          ViewBag.AddressId = new SelectList(db.tblAddresses, "AddressID", "Address1", tbluser.AddressId);
          ViewBag.BusinessCategoryId = new SelectList(db.tblBusinessCategories, "BusinessID", "BusinessName", tbluser.BusinessCategoryId);
          ViewBag.UserTypeId = new SelectList(db.tblUserTypes, "UserTypeID", "UserType", tbluser.UserTypeId);


          return View(tbluser);
      }

解决方案

First off, you have a redundant constraint that makes life harder:

Each business can have 1 workhours
Each user can have 1 business

By this logic the user has 1 workhours already so doesn't need the user.workhours fk!

I suggest you re-jig your table relationships to make life easier


这篇关于外键参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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