如何将StudentProfileID和Date Today组合为C#MVC5中Create Method的customID

时间:2015-03-14 09:20:27

标签: c# entity-framework asp.net-mvc-5 entity-framework-6

如何组合StudentProfileID和DateToday的自定义RegNo?

StudentProfileID-是Create Method上自动生成的主键
日期今天 - 格式:031415

创建方法的示例代码

public ActionResult Create([Bind(Include="StudentProfileID,RegNo,EnrollmentDate,LastName,FirstName,MiddleName,Address,ContactNo,MaritalStatus,Birthdate,Birthplace,Age,PositionRank,Years,PRC,SBOOK,MMAP,PPORT,SRC,TraineeType,EndorsedBy,EmergencyPerson,EmergencyAddress,EmergencyContactNo,EmergencyRelationship")] StudentProfile studentprofile)
    {
        if (ModelState.IsValid)
        {
            db.StudentProfile.Add(studentprofile);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        return View(studentprofile);
    }

我在创建学生档案时遵循了这篇文章 http://www.codeproject.com/Tips/869986/CRUD-operation-with-Entity-Framework-Code-First

我很感激你的帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

db.savechanges()之后;我们将获得sudentProfileId如下

 if (ModelState.IsValid)
        {
            db.StudentProfile.Add(studentprofile);
            db.SaveChanges();
            int studentId = studentprofile.StudentId;
            string custRegNo = studetId.toString()+ DateTime.Now;
            return RedirectToAction("Index");
        }