If you set them both as primary keys linq will remove the table from the diagram and just show a direct mapping... incase you dont know![]()
public static BlogEntry GetBlogEntry(int id)
{
using (BlogDataContext db = new BlogDataContext())
{
return db.BlogEntries.Where(b => b.BlogID == id).FirstOrDefault();
}
}
DateTime ds = Helper.BuildDateTime(txtDate.Text, TimePicker1.GetTextHour.ToString(), TimePicker1.GetTextMin.ToString(), TimePicker1.GetTextSecond.ToString());
BlogEntry be;
bool IsNew = false;
be = Blog.GetBlogEntry(Helper.QueryStringInt("id"));
if (be == null)
{
IsNew = true;
be = new BlogEntry();
}
if (Helper.QueryStringInt("id") >= 1)
be.BlogID = Helper.QueryStringInt("id");
be.BlogTitle = txtTitle.Text;
be.BlogText = txtContent.Text;
be.BlogDate = ds;
be.Deleted = false;
be.Published = chkPublish.Checked;
be.AllowComments = chkComments.Checked;
be.FriendlyURL = txtFriendlyURL.Text;
be.MetaDescription = txtMetaDescription.Text;
be.MetaTitle = txtMetaTitle.Text;
foreach (ListItem li in chkCategories.Items)
{
BlogCategoryMapping bcm = new BlogCategoryMapping();
bcm.BlogID = be.BlogID;
bcm.CategoryID = Convert.ToInt32(li.Value);
be.BlogCategoryMappings.Add(bcm);
}
BlogDataContext db = new BlogDataContext();
if (IsNew)
db.BlogEntries.InsertOnSubmit(be);
db.SubmitChanges();
db.Dispose();
be = Blog.GetBlogEntry(Helper.QueryStringInt("id"));
DateTime ds = Helper.BuildDateTime(txtDate.Text, TimePicker1.GetTextHour.ToString(), TimePicker1.GetTextMin.ToString(), TimePicker1.GetTextSecond.ToString());
BlogEntry be;
bool IsNew = false;
BlogDataContext db = new BlogDataContext();
be = db.BlogEntries.Where(b => b.BlogID == id).FirstOrDefault();
if (be == null)
{
IsNew = true;
be = new BlogEntry();
}
if (Helper.QueryStringInt("id") >= 1)
be.BlogID = Helper.QueryStringInt("id");
be.BlogTitle = txtTitle.Text;
be.BlogText = txtContent.Text;
be.BlogDate = ds;
be.Deleted = false;
be.Published = chkPublish.Checked;
be.AllowComments = chkComments.Checked;
be.FriendlyURL = txtFriendlyURL.Text;
be.MetaDescription = txtMetaDescription.Text;
be.MetaTitle = txtMetaTitle.Text;
foreach (ListItem li in chkCategories.Items)
{
BlogCategoryMapping bcm = new BlogCategoryMapping();
bcm.BlogID = be.BlogID;
bcm.CategoryID = Convert.ToInt32(li.Value);
be.BlogCategoryMappings.Add(bcm);
}
if (IsNew)
db.BlogEntries.InsertOnSubmit(be);
db.SubmitChanges();
db.Dispose();
What I mean is that when you look at one objects properties, you shouldn't have go to through the linking table (i.e. it will looke like BlogEntry -> BlogCategory rather than BlogEntry -> MappingTable -> BlogCategory). If done correctly the mapping table does not appear on the DataContext at all - its all implicit.They now show in the intellisense which wasnt happening before is that what you mean?
Unfortunately, all of the object update tracking is handled by the datacontext, so when you use your method GetBlogEntry, the datacontext that tracks that object changes is destroyed straight away. There is a method to attach an entity back to a data context but that doesn't seem to work.Also I have another question, this one a little more complicated.
...
Why is this?
What I mean is that when you look at one objects properties, you shouldn't have go to through the linking table (i.e. it will looke like BlogEntry -> BlogCategory rather than BlogEntry -> MappingTable -> BlogCategory). If done correctly the mapping table does not appear on the DataContext at all - its all implicit.
Firstly, the mapping table has to just be 2 columns... you can't have a primary key and then the 2 columns. Make them both primary keys (composite key). Then remove the 3 tables from your context, referesh server explorer and try again. When you drop the three of them on, only 2 should appear.
The one that got me when I was doing it was the fact it must be just 2 columns.