- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我必须缩短下面的代码。
我需要在列中生成并保存一个唯一的引用号,如 R00001
、R00002
、R00003
等。此处为 1, 2 & 3 基于身份 key 值。
为此,我编写了两次db.SaveChanges()
。首先,我保存数据并选择已保存的数据,并通过生成该唯一 key 来更新它。是否可以生成该 key 并在保存时立即传递?
因此,我不需要在保存并更新数据后通过再次调用 db.SaveChanges() 两次来选择数据。这是我的 MVC Controller (MVC 操作)代码。
[HttpPost]
public ActionResult Create(RateTable model, HttpPostedFileBase[] files)
{
RateTable objRC = null;
if (model.RatecontractId >= 1)
{
objRC = db.RateTables.FirstOrDefault(rc => rc.RatecontractId == model.RatecontractId);
}
if (objRC == null && model.RatecontractId <= 0)
{
objRC = new RateTable();
objRC.CountryCode = model.CountryCode;
objRC.CompanyCode = model.CompanyCode;
objRC.CustomerName = model.CustomerName;
objRC.Remarks = model.Remarks;
objRC.CreatedDate = DateTime.Now;
objRC.CreatedBy = User.Identity.Name;
//Saving data into the database table.
db.RateTables.Add(objRC);
db.SaveChanges();
string uniqueRefNo = "R" + string.Format("{0:D5}", objRC.RatecontractId);
int rateContractId = objRC.RatecontractId;
//For updating the unique reference number of the contract.
RateTable result = (from rc in db.RateTables where rc.RatecontractId == rateContractId select rc).SingleOrDefault();
if (result != null)
{
result.UniqueRefNo = uniqueRefNo;
db.SaveChanges();
}
}
}
我将数据保存在 UniqueRefNo
列中,如下所示:
RatecontractId CountryCd CompanyCd UniqueRefNo
---------------------------------------------------
1 C0U001 C0M001 R00001
2 C0U001 C0M001 R00002
3 C0U001 C0M001 R00003
4 C0U001 C0M001 R00004
5 C0U001 C0M001 R00005
我想减少此代码,因为我要保存的文件也需要以 R00001.pdf
、R00002.pdf
、R00003 等格式保存.pdf
等
这是文件保存代码,并根据唯一引用号重命名文件名。
if (files != null)
{
foreach (HttpPostedFileBase file in files)
{
if (file != null)
{
//To generate the file name of the rate contract.
var fileName = "RC" + string.Format("{0:D5}", objRC.RatecontractId);
var extension = Path.GetExtension(file.FileName);
fileName = fileName + extension;
string path = string.Format("~/Documents/PDF/{0}", fileName);
if (System.IO.File.Exists(Server.MapPath(path)))
System.IO.File.Delete(Server.MapPath(path));
file.SaveAs(Server.MapPath(path));
RateTable resultForfiles = (from rc in dbTender.RateTables where
rc.RatecontractId == objRC.RatecontractId select rc).SingleOrDefault();
if (resultForfiles != null && (extension.ToLower() == "pdf" || extension.ToLower() == ".pdf"))
{
resultForfiles.PdfFilePath = fileName;
}
if (resultForfiles != null && extension.ToLower() != "pdf" && extension.ToLower() != ".pdf")
{
resultForfiles.WordExcelFilePath = fileName;
}
dbTender.SaveChanges(); //Third thime calling `SaveChanges()`
}
}
}
最佳答案
作为一个选项,您可以创建一个标识列和一个计算列,在计算列中,根据标识列计算所需的值。例如:
CREATE TABLE [dbo].[Table1](
[Id] INT IDENTITY(1,1) NOT NULL PRIMARY KEY,
[Name] NVARCHAR(50) NOT NULL,
[Code] AS (('R'+ REPLICATE('0',(5) - LEN(CONVERT([NVARCHAR](50),[Id])))) +
CONVERT([NVARCHAR](50),[Id]))
)
然后,当您使用 EF 或这样的查询插入表时:
INSERT INTO [dbo].[Table1]([Name]) VALUES (N'Something')
结果是:
| Id | Name | Code |
.............................................
| 1 | Something | R00001 |
重要说明
Before choosing any solution, no matter computed column, sequence, or trigger,make sure you need to store such computed code in database. I think you don't need to generate and store the reference code at database side, it's enough to use thekey of the entity as a reference number / foreign key wherever alogical or physical relation is needed.
使用身份列:您可以定义身份Id
列。然后,在数据库中插入 yourEnitty
后,在 SaveChanges()
之后,yourEntity.Id
将包含新插入的标识值,您不需要对 db 的额外调用。然后您可以使用相同的 ID 作为引用(例如文件名)。
使用 Guid 列:您可以定义 Guid Id
列。然后在数据库中插入 yourEnitty
之前,将 Guid.NewId()
分配给 Id
列,然后您可以使用相同的 guid 作为引用(例如例如文件名)。
考虑以下几点:
我的建议是使用 key (例如 GUID key )存储和检索文件。然后,在下载操作中,您可以轻松发送具有不同名称的文件进行下载,例如:
public ActionResult Download(Guid id)
{
var file = Server.MapPath($"~/Content/uploads/{id}.pdf";
return File(file, "application/pdf", "something.pdf");
}
关于c# - 如何根据表的最大标识键使用 Entity Framework 在 MVC 中保存数据时生成唯一编号?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60016398/
我有这些实体(这只是我为这篇文章创建的抽象): 语言 区 说明 这些是它们之间的引用: 区 * - 1 语言 说明 * - 1 语言 区 1 - 1 说明 如果我这样取: var myFetch =
经过大量谷歌搜索后,除了降级 hibernate 版本之外,我没有找到问题的答案。但我在 2003 年类似的帖子中遇到了这种情况。 问题是什么: //in the first session I d
我听说过 linq to entities 。 Entity Framework 是利用linq to entities吗? 最佳答案 LINQ to Entities 是 Entity Framew
我是 Entity Framework 和 ASP.Net MVC 的新手,主要从教程中学习,对任何一个都没有深入了解。 (我确实有 .Net 2.0、ADO.Net 和 WebForms 方面的经验
如果我编写 LINQ to Entities 查询,该查询是否会转换为提供程序理解的 native 查询(即 SqlClient)? 或者 它是否会转换为实体 SQL,然后 Entity Framew
这个问题已经有答案了: EF: Include with where clause [duplicate] (5 个回答) 已关闭 2 年前。 看来我无法从数据库中获取父级及其子级的子集。 例如...
我开始在一家新公司工作,我必须在一个旧项目上使用 C++ 工作。所以,我忘记了一些 C++ 本身的代码结构。在一个函数中,我在一个函数中有双冒号::,但我不知道如何理解它。 例如,我知道如果我有 EN
我写了一个方法来允许为 orderby 子句传递一个表达式,但我遇到了这个问题。 Unable to cast the type 'System.DateTime' to type 'System.I
简单的问题:LINQ to Entities 和 Entity Framework 有什么区别?到目前为止,我认为这两个名称是用来描述同一个查询的,但我开始觉得事实并非如此。 最佳答案 Entity
我想使用 Entity Framework 。但是,我还要求允许我的用户在我们的系统中定义自定义字段。我想仍然使用 Entity Framework ,而不是使用具有哈希表属性的分部类。 下面是我想到
我正在阅读这个 E.F. 团队博客的这个系列 http://blogs.msdn.com/b/adonet/archive/2011/01/27/using-dbcontext-in-ef-featu
我正在使用 EF6 开发插件应用程序,代码优先。 我有一个名为 User 的实体的主要上下文。 : public class MainDataContext : DbContext { pub
当我得到最后的 .edmx 时,我遇到了问题。 我收到一条消息说 错误 11007:未映射实体类型“pl_Micro”。 查看设计器 View ,我确实看到该表确实存在。 我怎样才能克服这个消息? 最
我已阅读与使用 Entity Framework 时在 Linq to Entities (.NET 3.5) 中实现等效的 LEFT OUTER JOIN 相关的所有帖子,但尚未找到解决以下问题的方
使用 WCF RIA 服务和 Entity Framework 4. 我有 3 个 DTO:学校、州、区。 州 DTO 有一个地区属性(property),其构成。学校 DTO 有一个国家属性(pro
我有一个 Employee 实体,它继承自一个继承自 Resource 实体(Employee -> Person -> Resource)的 Person 实体。是否可以通过编程方式获取 Emplo
我有一个使用 JPA 的 java 应用程序。 假设我有一个名为 Product 的实体与 name和 price属性(所有实体都有一个 id 属性)。 自然我可以得到一个List相当容易(来自查询或
我有一个 Entity Framework 类,其中有两个指向另一个对象的引用 public class Review { [Key] public int Id {get;s
我是 Symfony 2 的新手,我想知道一些事情: 假设我的项目中有 2 个 bundle 。我想在两个包中使用从我的数据库生成的实体。 我应该在哪里生成实体? (对我来说,最好的方法是在 bund
我想在具有方法和属性的部分类中扩展 EF 实体。我经常这样做。 但是现在我需要将来自该实体的数据与来自其他实体的数据结合起来。因此,我需要能够访问实体 objectcontext(如果附加)来进行这些
我是一名优秀的程序员,十分优秀!