发行与实体框架/ DB关系
我有一个类文章:
public class Article
{
public int Id { get; set; }
public string Text { get; set; }
public Title Title { get; set; }
}
和标题:
public class Title
{
public int Id { get; set; }
public string Name { get; set; }
public int MaxChar { get; set; }
}
在你可以写一个文章
,你要选择你的标题
从列表,让你StringLength为 Article.Text
确定即可。意思是,这篇文章只能有一定量的字符,deppending什么标题的作家了。例如: Title.Name
标题1只能写1000个字符的文章( MaxChar
)和 Title.Name
标题2可以写一篇文章3000字符。所以。那意味着 Article.Text
的字符串长度必须来自 Title.MaxChar
。
Before you can write an Article
, you have to choose your Title
from a list, so your StringLength for Article.Text
can be determined. Meaning, this article can only have a certain amount of chars, deppending on what 'Title' the writer has. Example: Title.Name
"Title1" can only write an article with 1000 chars (MaxChar
), and Title.Name
"Title2" can write an article with 3000 chars. So. Thats means the the string length for Article.Text
has to come from Title.MaxChar
.
的标题
实体将被存储在DB prefixed数据。
The Title
entity is prefixed data that will be stored in the db.
下面是香港专业教育学院做了什么曾根远:
从数据库的标题是在视图中列出,链接,创建一个称号查询字符串ArticleController的动作:
Here's what ive done sone far: The titles from the db are listed in a view, with a link to create action of the ArticleController with a "title" querystring:
@Models.Title
@foreach (var item in Model) {
@Html.ActionLink(item.Name, "Create", "Article", new { title = item.Id}, new FormMethod())
}
您填写表格,并张贴。该HttpPost创建行动:
You fill the form, and post it. The HttpPost Create action:
[HttpPost]
public ActionResult Create(Article article)
{
if (article.Text.Length > article.Title.MaxChar)
{
ModelState.AddModelError("Text",
string.Format("The text must be less than {0} chars bla bla", article.Title.MaxChar));
}
if (ModelState.IsValid)
{
db.Article.Add(article);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(hb);
}
这里的问题。该控制器还增加了新的标题
实体。因此,下一次我浏览到我必须选择标题来看,有我以前写的一篇文章的最后实体的副本。
Here's the issue. The controller also adds a new Title
entity. So the next time I navigate to the view where I have to choose Title, there's a duplicate of the last entity I used to write an article.
我应该这样做在entirly新的方式,还是有一个小调整。唯一的其他东西我能想到的,只是在发送 MaxChar
作为查询字符串,并在所有的模型之间无关。只是似乎有点傻/ web表单kindda。
Should I do this in an entirly new way, or is there a small tweak. Only other thing I can think of, is just sending the MaxChar
as a querystring, and have no relations between the models at all. Just seems a bit silly/webforms kindda.
干杯
更新#1:
也许我这样做了错误的方式?
获取创建行动
UPDATE #1: Maybe im doing this the wrong way? Get Create action
public ActionResult Create(int title)
{
var model = new Article
{
Title = db.Title.Find(title)
};
return View(model);
}
或者,也许它的示范?就像,我必须设置外键?是这样的:
Or maybe its in the Model? Like, do I have to set foreign keys? Something like:
[ForeignKey("Title")]
public int MaxChar { get; set; }
public virtual Title Title { get; set; }
但IM pretty肯定我看了一些地方认为它不是necesary,即EF需要的照顾。
But im pretty sure I read some where that it isnt necesary, that EF takes care of that.
最简单的方法很可能是标题附加到你的创建
行动的背景:
Easiest way would probably be to attach the title to the context in your Create
action:
// ...
if (ModelState.IsValid)
{
db.Titles.Attach(article.Title);
db.Article.Add(article);
db.SaveChanges();
return RedirectToAction("Index");
}
// ...
附加
告诉EF的 article.Title
已存在于数据库中,从而避免了一个新的标题插入。
Attach
tells EF that article.Title
already exists in the database, thereby avoiding that a new Title
is inserted when you add the article to the context.