LINQ2SQL:更新引用另一个实体的实体字段
我在 LINQ 中遇到以下情况:我有一个对象(博客“Post”),它有一个“Status”字段,可以采用不同的值,并且有一个表列出了所有这些值(该表只有 Id、Desc) .
现在,由于创建了两个表之间的关系,因此在模型中,Post.Status 不是 int,而是“PostStatus”的实例。
因此,如果我想将帖子的状态设置为 20,我不能只将字段设置为 20,我需要有一个 PostStatus 实例来分配。这些状态值都是用逻辑硬编码的,所以在我的代码中硬编码“20”就可以了。
还有比这更好的方法吗?
switch (Action) {
case "Ignore":
post.PostStatus = (from s in db.PostStatus where s.Id == 90 select s).First();
break;
case "Assign":
post.PostStatus = (from s in db.PostStatus where s.Id == 20 select s).First();
break;
case "MarkDone":
post.PostStatus = (from s in db.PostStatus where s.Id == 30 select s).First();
break;
case "MarkPublished":
post.PostStatus = (from s in db.PostStatus where s.Id == 40 select s).First();
post.Public = true;
break;
}
老实说,我讨厌这段代码,首先是因为它不必要地查询数据库来获取 PostStatus 实例,但主要是因为它太冗长了。
有更好的方法吗?
谢谢!
丹尼尔
I have the following situation in LINQ: I have an object (a blog "Post") that has a "Status" field which can take on different values, and there's a table listing all these values (this table just has Id, Desc).
Now, since the relationship between both tables is created, in the Model, Post.Status is not an int, but an instance of "PostStatus".
Thus, if I want to set the status of a Post to, say, 20, I can't just set the field to 20, I need to have an instance of PostStatus to assign. These status values are all hardcoded with the logic, so it's just fine to have "20" hardcoded in my code.
Is there a better way to do it that this?
switch (Action) {
case "Ignore":
post.PostStatus = (from s in db.PostStatus where s.Id == 90 select s).First();
break;
case "Assign":
post.PostStatus = (from s in db.PostStatus where s.Id == 20 select s).First();
break;
case "MarkDone":
post.PostStatus = (from s in db.PostStatus where s.Id == 30 select s).First();
break;
case "MarkPublished":
post.PostStatus = (from s in db.PostStatus where s.Id == 40 select s).First();
post.Public = true;
break;
}
I hate this code, honestly, first of all because it needlessly queries the DB to get a PostStatus instance, but mostly because it's just so verbose.
Is there a better way to do this?
Thanks!
Daniel
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
听起来 PostStatus 或多或少是一个枚举。我们总是通过在数据库中拥有一个相关表(就像您所做的那样)来执行类似的操作,然后删除 DBML 中的关系并将子列上的 CLR 类型更改为枚举类型。枚举类型使用与父数据库表中的行相同的值进行声明(在您的情况下,Assign = 20、MarkDone = 30 等)。我编写了一个工具,可以从我们所有枚举的代码中填充表的值 - 这样代码就是“主”(如果有人试图更改现有值的值或名称,它会爆炸,但新值“正常工作” )。现在,当您想做类似您正在做的事情时,您需要的数据已经存在于枚举中 - 只需设置 Post.PostStatus = Action (假设您还将 Action 从字符串更改为相同的枚举类型) - 没有数据库打必要的。
我们还有一些更加动态的东西——我们在应用程序启动时从数据库缓存一次并放入字典中,这样我们就可以在没有数据库命中的情况下设置ID。只要您不谈论数百万个值,它就非常有效。
It sounds like PostStatus is more-or-less an enum. We do things like this all the time by having a related table in the DB (as you do), but then remove the relationship in the DBML and change the CLR type on the child column to an enum type. The enum type is declared with the same values as the rows in the parent DB table (in your case, Assign = 20, MarkDone = 30, etc). I wrote a tool that fills the table's values from the code for all our enums- that way the code is the "master" (it bombs if someone tries to change a value or name of an existing value, but new values "just work"). Now when you want to do something like what you're doing, the data you need is already there in the enum- just set Post.PostStatus = Action (assuming you also change Action from a string to the same enum type)- no DB hit necessary.
We also have some of these that are a little more dynamic- those we cache once from the DB on app startup and drop into a Dictionary so we can just set the IDs without the DB hit. As long as you're not talking about millions of values, it's very efficient.