I'm making good progress on my new Government Content Management System (Govern CMS).
Probably have another 1-2 weeks of development before I have a minimum viable product.
Here is my Entity Framework EDMX for that System:
Showing posts with label Entity Framework. Show all posts
Showing posts with label Entity Framework. Show all posts
Friday, September 8, 2017
Friday, August 4, 2017
Back on another project and new EDMX
Finally settled on a 3rd Party Mail Sender: SendGrid.
Out of all the ones I tried:
- SendGrid
- MailGun
- SparkPost
SendGrid is the only one that has consistently sent out my emails. Which is an important feature of a 3rd party emailer.
Plus, Azure provides free sending of SendGrid Emails with your subscription.
If Azure provided SMTP servers and a mail infrastructure, I totally would have just handled this in-house. However, they outsourced this to SendGrid.
My new EDMX. Still elegant, but growing:
Saturday, July 15, 2017
Entity Framework Woes
I started getting a new and horrifying Entity Framework error I had never seen before.
Conflicting changes detected. This may happen when trying to insert multiple entities with the same key.
It looked like it was due to me saving a parent and having it cascade to save children.
Soooo, I took it upon myself to save parent, save child, reconstitute relationship.
This was horrifying. My code went from Exhibit A:
///
/// Helper Method. Updates Sections and Items
///
/// Agenda ID
/// List of AgendaSections
/// The Current User creating Sections and Items
private void UpdateSectionsAndItems(int agendaId, IList<AgendaSection> agendaSections, IUser currentUser)
{
List<AgendaSectionItem> agendaSectionItems = new List<AgendaSectionItem>();
int sectionNumber = 0;
foreach (AgendaSection section in agendaSections)
{
section.Number = sectionNumber;
section.AgendaId = agendaId;
section.OwnerId = currentUser.UserId;
if (section.AgendaSectionItems != null)
{
int itemNumber = 0;
foreach (AgendaSectionItem item in section.AgendaSectionItems)
{
item.Number = itemNumber;
itemNumber++;
item.OwnerId = currentUser.UserId;
agendaSectionItems.AddRange(section.AgendaSectionItems);
}
}
sectionNumber++;
}
db.AgendaSections.AddRange(agendaSections);
db.AgendaSectionItems.AddRange(agendaSectionItems);
db.SaveChanges();
}
To the following horrifying monstrosity:
///
/// Helper Method. Updates Sections and Items
///
/// Agenda ID
/// List of AgendaSections
/// The current user
private void UpdateSectionsAndItems(int agendaId, IList<AgendaSection> agendaSections, IUser currentUser)
{
// This is a horrible workaround to deal with this mysterious error:
// Conflicting changes detected. This may happen when trying to insert multiple entities with the same key.
// That I get when I simply try to save parent (AgendaSection) and let it cascade down to the child (AgendaSectionItems).
IDictionary<int, List<AgendaSectionItem>> tempItemDictionary = new Dictionary<int, List<AgendaSectionItem>>();
int sectionNumber = 0;
foreach (AgendaSection section in agendaSections)
{
section.Number = sectionNumber;
section.AgendaId = agendaId;
if (section.AgendaSectionItems != null)
{
int itemNumber = 0;
foreach (AgendaSectionItem item in section.AgendaSectionItems)
{
item.Number = itemNumber;
itemNumber++;
item.OwnerId = currentUser.UserId;
if (tempItemDictionary.ContainsKey(section.Number.GetValueOrDefault()))
{
List<AgendaSectionItem> agendaSectionItems = tempItemDictionary[section.Number.GetValueOrDefault()];
agendaSectionItems.Add(item);
}
else
{
List<AgendaSectionItem> agendaSectionItems = new List<AgendaSectionItem>();
agendaSectionItems.Add(item);
tempItemDictionary.Add(section.Number.GetValueOrDefault(), agendaSectionItems);
}
}
}
// More workaround
section.AgendaSectionItems = null;
sectionNumber++;
}
db.AgendaSections.AddRange(agendaSections);
db.SaveChanges();
// More workaround ... FML
foreach (int i in tempItemDictionary.Keys)
{
IList<AgendaSectionItem> items = tempItemDictionary[i];
AgendaSection agendaSection = agendaSections.First(s => s.Number == i);
if (agendaSection != null)
{
foreach (AgendaSectionItem item in items)
{
item.SectionId = agendaSection.SectionId;
if (agendaSection.AgendaSectionItems == null)
{
agendaSection.AgendaSectionItems = new List<AgendaSectionItem>();
}
agendaSection.AgendaSectionItems.Add(item);
db.AgendaSectionItems.Add(item);
db.SaveChanges();
}
}
}
}
When I finally got down to it, what I had done was accidentally mapped my primary key on a table as a foreign key in a completely incorrect mapping. So, I went and rolled back the monstrosity and will sleep better tonight.
Wednesday, July 12, 2017
Introducing: new EDMX and a personal challenge
I am taking a break to build a Recruiter Review Website.
The Data Model is much simpler:
I am a firm believer that a good Senior Developer should be able to go into a 1 hour Interview and build a fully functional website.
I am challenging myself to build this new site in 1 day. A 1 hour Website should be functional, but will not look polished and have nice widgets and UI, but I really do believe a good Senior Developer should be able to build a functional CRUD site in an hour.
The Data Model is much simpler:
I am a firm believer that a good Senior Developer should be able to go into a 1 hour Interview and build a fully functional website.
I am challenging myself to build this new site in 1 day. A 1 hour Website should be functional, but will not look polished and have nice widgets and UI, but I really do believe a good Senior Developer should be able to build a functional CRUD site in an hour.
Monday, July 10, 2017
Introducing: my EDMX Diagram
I am building my own Software Project.
Here is my Data Model (Entity Framework EDMX Diagram).
You know ... that horrifying moment when you ask yourself "is my Data Model too complex?"
Seriously though, I went with the simplest Data Model possible given the scope and functionality of my site, and keeping with 3rd Normal Form, and this is what I arrived at.
Here is my Data Model (Entity Framework EDMX Diagram).
You know ... that horrifying moment when you ask yourself "is my Data Model too complex?"
Seriously though, I went with the simplest Data Model possible given the scope and functionality of my site, and keeping with 3rd Normal Form, and this is what I arrived at.
Saturday, December 5, 2009
Entity Framework: EDMX
My previous post showed how to quickly generate the EDMX file and corresponding codebehind classes using VS.NET 2008.
The EDMX itself contains the contents of core model/mapping files for Entity Framework:
Here is an article from the ADO.NET team blog on Extracting CSDL from EDMX.
Here is my generated EDMX file:
Here is how it appears in Visual Studio Design View:
The EDMX itself contains the contents of core model/mapping files for Entity Framework:
- Conceptual schema definition language (.csdl) - defines the conceptual model.
- Store schema definition language file (.ssdl) - defines the storage model, aka logical model.
- Mapping specification language file (.msl) - defines the mapping between the storage and conceptual models.
Here is an article from the ADO.NET team blog on Extracting CSDL from EDMX.
Here is my generated EDMX file:
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
<!-- EF Runtime content -->
<edmx:Runtime>
<!-- SSDL content -->
<edmx:StorageModels>
<Schema Namespace="PersonModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl">
<EntityContainer Name="PersonModelStoreContainer">
<EntitySet Name="Person" EntityType="PersonModel.Store.Person" store:Type="Tables" Schema="dbo" />
<EntitySet Name="Profile" EntityType="PersonModel.Store.Profile" store:Type="Tables" Schema="dbo" />
<AssociationSet Name="FK_Profile_Person" Association="PersonModel.Store.FK_Profile_Person">
<End Role="Person" EntitySet="Person" />
<End Role="Profile" EntitySet="Profile" />
</AssociationSet>
</EntityContainer>
<EntityType Name="Person">
<Key>
<PropertyRef Name="PersonID" />
</Key>
<Property Name="PersonID" Type="bigint" Nullable="false" />
<Property Name="LoginID" Type="varchar" Nullable="false" MaxLength="100" />
<Property Name="Password" Type="varchar" Nullable="false" MaxLength="32" />
</EntityType>
<EntityType Name="Profile">
<Key>
<PropertyRef Name="ProfileID" />
</Key>
<Property Name="ProfileID" Type="bigint" Nullable="false" />
<Property Name="PersonID" Type="bigint" Nullable="false" />
<Property Name="FirstName" Type="nvarchar" Nullable="false" MaxLength="100" />
<Property Name="LastName" Type="nvarchar" Nullable="false" MaxLength="100" />
<Property Name="EmailAddress" Type="nvarchar" Nullable="false" MaxLength="255" />
</EntityType>
<Association Name="FK_Profile_Person">
<End Role="Person" Type="PersonModel.Store.Person" Multiplicity="1" />
<End Role="Profile" Type="PersonModel.Store.Profile" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Person">
<PropertyRef Name="PersonID" />
</Principal>
<Dependent Role="Profile">
<PropertyRef Name="PersonID" />
</Dependent>
</ReferentialConstraint>
</Association>
</Schema>
</edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
<Schema Namespace="PersonModel" Alias="Self" xmlns="http://schemas.microsoft.com/ado/2006/04/edm">
<EntityContainer Name="PersonEntities">
<EntitySet Name="Person" EntityType="PersonModel.Person" />
<EntitySet Name="Profile" EntityType="PersonModel.Profile" />
<AssociationSet Name="FK_Profile_Person" Association="PersonModel.FK_Profile_Person">
<End Role="Person" EntitySet="Person" />
<End Role="Profile" EntitySet="Profile" />
</AssociationSet>
</EntityContainer>
<EntityType Name="Person">
<Key>
<PropertyRef Name="PersonID" />
</Key>
<Property Name="PersonID" Type="Int64" Nullable="false" />
<Property Name="LoginID" Type="String" Nullable="false" MaxLength="100" Unicode="false" FixedLength="false" />
<Property Name="Password" Type="String" Nullable="false" MaxLength="32" Unicode="false" FixedLength="false" />
<NavigationProperty Name="Profile" Relationship="PersonModel.FK_Profile_Person" FromRole="Person" ToRole="Profile" />
</EntityType>
<EntityType Name="Profile">
<Key>
<PropertyRef Name="ProfileID" />
</Key>
<Property Name="ProfileID" Type="Int64" Nullable="false" />
<Property Name="FirstName" Type="String" Nullable="false" MaxLength="100" Unicode="true" FixedLength="false" />
<Property Name="LastName" Type="String" Nullable="false" MaxLength="100" Unicode="true" FixedLength="false" />
<Property Name="EmailAddress" Type="String" Nullable="false" MaxLength="255" Unicode="true" FixedLength="false" />
<NavigationProperty Name="Person" Relationship="PersonModel.FK_Profile_Person" FromRole="Profile" ToRole="Person" />
</EntityType>
<Association Name="FK_Profile_Person">
<End Role="Person" Type="PersonModel.Person" Multiplicity="1" />
<End Role="Profile" Type="PersonModel.Profile" Multiplicity="*" />
</Association>
</Schema>
</edmx:ConceptualModels>
<!-- C-S mapping content -->
<edmx:Mappings>
<Mapping Space="C-S" xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS">
<EntityContainerMapping StorageEntityContainer="PersonModelStoreContainer" CdmEntityContainer="PersonEntities">
<EntitySetMapping Name="Person">
<EntityTypeMapping TypeName="IsTypeOf(PersonModel.Person)">
<MappingFragment StoreEntitySet="Person">
<ScalarProperty Name="PersonID" ColumnName="PersonID" />
<ScalarProperty Name="LoginID" ColumnName="LoginID" />
<ScalarProperty Name="Password" ColumnName="Password" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
<EntitySetMapping Name="Profile">
<EntityTypeMapping TypeName="IsTypeOf(PersonModel.Profile)">
<MappingFragment StoreEntitySet="Profile">
<ScalarProperty Name="ProfileID" ColumnName="ProfileID" />
<ScalarProperty Name="FirstName" ColumnName="FirstName" />
<ScalarProperty Name="LastName" ColumnName="LastName" />
<ScalarProperty Name="EmailAddress" ColumnName="EmailAddress" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
<AssociationSetMapping Name="FK_Profile_Person" TypeName="PersonModel.FK_Profile_Person" StoreEntitySet="Profile">
<EndProperty Name="Person">
<ScalarProperty Name="PersonID" ColumnName="PersonID" />
</EndProperty>
<EndProperty Name="Profile">
<ScalarProperty Name="ProfileID" ColumnName="ProfileID" />
</EndProperty>
</AssociationSetMapping>
</EntityContainerMapping>
</Mapping>
</edmx:Mappings>
</edmx:Runtime>
<!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
<edmx:Designer xmlns="http://schemas.microsoft.com/ado/2007/06/edmx">
<edmx:Connection>
<DesignerInfoPropertySet>
<DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" />
</DesignerInfoPropertySet>
</edmx:Connection>
<edmx:Options>
<DesignerInfoPropertySet>
<DesignerProperty Name="ValidateOnBuild" Value="true" />
</DesignerInfoPropertySet>
</edmx:Options>
<!-- Diagram content (shape and connector positions) -->
<edmx:Diagrams>
<Diagram Name="Person">
<EntityTypeShape EntityType="PersonModel.Person" Width="1.5" PointX="0.75" PointY="1" Height="1.592306315104167" IsExpanded="true" />
<EntityTypeShape EntityType="PersonModel.Profile" Width="1.5" PointX="3" PointY="0.875" Height="1.7566536458333333" IsExpanded="true" />
<AssociationConnector Association="PersonModel.FK_Profile_Person" ManuallyRouted="false">
<ConnectorPoint PointX="2.25" PointY="1.7961531575520835" />
<ConnectorPoint PointX="3" PointY="1.7961531575520835" /></AssociationConnector></Diagram></edmx:Diagrams>
</edmx:Designer>
</edmx:Edmx>
Here is how it appears in Visual Studio Design View:
Entity Framework Quick Start Tutorial
Entity Framework is an OR/M that began shipping with .NET Framework 3.5 SP1 as part of ADO.NET.
In order to begin working with Entity Framework in VS.NET 2008, right click on a Project and select Add New Item.
Under the New Item dialog window, select data, and choose ADO.NET Entity Data Model.

This will create the EDMX file and corresponding codebehind classes.
Next, we need to choose the model contents, which will either start from a database source, or be a blank, new model.

I chose "Generate from Database Source", the quickest and easiest way to get up and running.
From here, you need to provide information about a database connection and database:

Based on the information provided, you will be given the option to choose database objects to include in the model.

Finally, this will create the EDMX file and corresponding codebehind partial classes for the model.
In order to begin working with Entity Framework in VS.NET 2008, right click on a Project and select Add New Item.
Under the New Item dialog window, select data, and choose ADO.NET Entity Data Model.

This will create the EDMX file and corresponding codebehind classes.
Next, we need to choose the model contents, which will either start from a database source, or be a blank, new model.

I chose "Generate from Database Source", the quickest and easiest way to get up and running.
From here, you need to provide information about a database connection and database:

Based on the information provided, you will be given the option to choose database objects to include in the model.

Finally, this will create the EDMX file and corresponding codebehind partial classes for the model.
Subscribe to:
Posts (Atom)


