Entity Framework assications (Independent vs FK)
Entity Framework association is a different concept to FKs in the database schema. Supporting or not supporting the Schema FKs has been a topic for debate in the community until now. Faisal Mohamood (Program Manager of Entity Framework) believes in the following prons and cons of using FKs in a model (Ref 1):
Benefits of Foreign Keys
- Keeps it simple (for the simple cases) and allows you to deal with relationship like you deal with them in the database
- Technically, you can update relationships without having both ends loaded/materialized. This is however in reality not always interesting since you will likely load both ends but this feature is definitely useful.
Disadvantages of Foreign Keys in the Model
- It is a part of the impedance mismatch problem.
- It doesn’t allow the concepts that you would expect from relationships in objects (easily getting from one end to the other) for instance.
- Having foreign keys as well as object references for relationship navigation presents the problem of two different artifacts representing relationships – this introduces complexity and now you have to make sure that you keep these two in sync.
I think the biggest disadvantage of FKs (stated in number 2) is the conflict with the object model pattern.
However, it seems that Microsoft decided that both sides of this discussion have their valid points. So in Entity Framework .Net 4, Fks are supported and it is up to the software architects to make their decision to just use independent associations (.Net 3.5 SP1) or add the FKs to the model.
The following code snippets form ADO.Net blog illustrate the implication of using Fks as part of the model (Ref 2). In our model there are two entities Product and Category, with the association one to many where each Product belongs to one Category.
First: Using the independent association between Product and Category.
public void Create_new_Product_in_existing_Category_by_reference()
{
using (var context = new Context())
{
//Create a new product and relate to an existing category
Product p = new Product
{
ID = 1,
Name = "Bovril",
Category = context.Categories
.Single(c => c.Name == "Food")
};
// Note: no need to add the product, because relating
// to an existing category does that automatically.
// Also notice the use of the Single() query operator
// this is new to EF in .NET 4.0 too.
context.SaveChanges();
}
}
Second: Using the Foreign Key.
using (var context = new Context())
{
//Create a product and a relationship to a known category by ID
Product p = new Product
{
ID = 1,
Name = "Bovril",
CategoryID = 13
};
//Add the product (and create the relationship by FK value)
context.Products.AddObject(p);
context.SaveChanges();
}
Further Reading:
- [MSDN] Defining and Managing Relationships (Entity Framework)
- Inheritance and Associations with Entity Framework Part 3
References:
Make the ASP:Login control centre
Following my previous post you cannot use “p” or “div” tags to align a ASP:Login control to the center of the page. So to get this done, you need to set the margin property of the control to “auto”.
<body>
<form id="form1" runat="server">
<div>
<asp :login runat="server" Style="margin:auto"></asp></div>
</form>
</body>
Align a table in the middle of an HTML | XHTML page
In the past it was common to align a table in the middle of a HTML page using the following code:
<div align="center">
<table>
<tr>
<td>
Test String</td>
</tr>
</table>
</div>
However, this is no longer working as in the new XHTML standards “align” is not a valid attribute of
<table style="margin: auto;">
<tr>
<td>
Test String
</td>
</tr>
</table>
Visual Studio Setup (deployment) project
Reading the Microsoft support article about using the Setup project in Visual Studio:
In order to create a Setup deployment project :
- Start a new project by doing one of the following:
- On the File menu, point to New, and then click Project.
-or-
- If you have an project open that you would like to create a setup package for, right-click Solution MyProject (where the name of your project is MyProject) in the Solution Explorer, point to Add, and then click New Project.
- In the New Project dialog box, select Setup and Deployment Projects in the Project Type pane, and then select the type of setup that you want in the Templates pane.
The project is added to the Solution Explorer, and then the File System Editor opens.
- In the Properties dialog box, select the ProductName property, and then type the name of your product.
And to add files to the Setup project:
- In the File System Editor, select the Application Folder node.
- Right-click the Application folder, and then on the Action menu, click Add, File. In the Add Files dialog box, browse to select any files that you need to add to your application.
NOTE: If you already have an application project in your solution, you can add the project outputs by selecting Project Outputs instead of File.
- To add an existing merge module to your setup package (this is not possible for a Cab project), right-click the name of your setup package in the Solution Explorer. Click Add, and then click Merge Module. In the Add Modules dialog box, browse to select any merge modules that you need to add to your application.
To create a shortcut for the installed program:
- Open the File System Editor: from the View menu, point to Editor, and then click File System.
- Open the application folder that contains the file that you want to create a shortcut for.
- Right-click the file that you want to create a shortcut to.
- Click Create Shortcut.
- A shortcut will be created in the same folder as the original file.
- Drag the shortcut to the desired folder. For example, if you want to create a shortcut in your Program menu, drag the shortcut to the Programs menu folder; if you want to create a shortcut on the Start menu, drag the shortcut to the Start menu folder. The folder location of the shortcut can also be changed through the folder field of the Properties Window.
To register COM objects as part of the installation:
Add a COM object to your Visual Studio deployment project. In the Solution Explorer, right-click the module that you just added, and then click Properties. NOTE: The Properties window contains a table with two columns and x number of rows (the number of rows depends on the project). The left column lists the specific properties. The right column is explained in step 4.
Go to Properties for this module (located by default in the upper-right corner of the .NET Deployment project), and then click Registry property. NOTE: The Registry property specifies whether a file, assembly, or project output group should be registered on a target computer during installation.
There is a list box in the right column of the Registry property, which displays several options for you to choose from. Note the following details for an explanation of these options:
- For assembly, registration is not normally required, and therefore the default is DoNotRegister (this means that the item will not be registered during the installation).
- For a COM module, you have the options of COM, COMRelativePath, and COMSelfReg. Any one of those three options will register the COM module during the installation.
Note the following details about each choice:
- COM: The module will be registered as a COM object by the Windows Installer engine. The deployment project will update the Class table, ProgID table, and other tables in the Registry Tables group of the corresponding .msi file. This is the recommended way to register a COM module.
- COMRelativePath: The module will be registered as an isolated COM object by the Windows Installer engine. Note that this module will be used only by the application that the module is installed with.
- COMSelfReg: The installer calls the DllRegisterServer function of that module at the time that you install the module and the DllUnregisterServer function at the time that you uninstall the module. The deployment project will update the SelfReg table of the corresponding .msi file. It is not recommended that the installation package use self-registration. Instead, the installation package should register modules by authoring one or more of the other tables provided by the installer for this purpose (that is, select the COM or COMRelativePath options). Many of the benefits of having a central installer service are lost with self-registration, because self-registration routines tend to hide critical configuration information.
Referemces:
States pane not visible in Blend 3
When I started to follow some of the video tuturial for Expression Blend 3, I noticed that States pane is not available when I work on the WPF project; however, it is there when I create a Silverlight project. Apparetly the state manager is part of WPFToolkit and the work arround this issue is explained in team blog of the Expression Blend:
Here are the steps you need to follow to get to the VSM-for-WPF goodness:
- Install the WPF Toolkit from http://www.codeplex.com/wpf/Release/ProjectReleases.aspx?ReleaseId=15598
- Create the following registry value and make it non-zero. The value should be of type DWORD. Or run the appropriate .REG file attached to this post (or by clicking here).
- 32 bit OS : HKLM/Software/Microsoft/Expression/Blend/EnableVSM
- 64 bit OS : HKLM/Software/Wow6432Node/Microsoft/Expression/Blend/EnableVSM
- If Blend was running during the previous step then restart it.
- Create a new WPF project.
- Add %Program Files%\WPF\WPF Toolkit\…\WPFToolkit.dll to the References folder (Right-click the References node in Project, then click Add Reference…).
- Close and reopen Window1.xaml. The States pane will now appear for the project.