AdventureWorks can't create ado.net entity data model - sql-server

I'm trying to use microsoft's sample database AdventureWorks2008R2... when I try to create the ADO.NET entity data model I get this error:
Unable to generate the model because of the following exception: 'The table 'C:\USERS\XXXX\DOCUMENTS\VISUAL STUDIO 2010\PROJECTS\ANOTHERWORKS\ANOTHERWORKS\APP_DATA\ADVENTUREWORKS2008R2_DATA.MDF.Production.Document' was referenced by a relationship, but was not found.'.
Loading metadata from the database took 00:00:06.2308687.
Generating the model took 00:00:04.5808698.
Added the connection string to the Web.Config file.
Successfully registered the assembly 'System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' in the Web.Config file.
Writing the .edmx file took 00:00:00.0015898.
Anyone encountered and fixed this? Or knows somewhere I can download a working version of this database ( other then: http://msftdbprodsamples.codeplex.com/releases/view/59211 which is where I got it )
Or can someone point to a sample database I can download and use around with entity framework.

EDIT
The new EF designer (Beta 1 available here) will not try creating relationships to non-existing tables so instead of the error and empty model you will get a model where invalid entity types/sets and relationships are commented out.
**
I looked at it for AdventureWorks for Sql Server 2012 but I think it is the same thing you hit for 2008R2/
The issue here is that the Production.Document table has a key that is of HierarchyId type and EF currently does not support HierarchyId type. EF ignores columns of types it does not understand when creating the model however if it does not understand a key column it will exclude the entire entity from the model. For excluded entities you should be able to find them commented out in the model when you open it with an Xml/Text editor. In this particular case this is what will see:
<EntityContainer Name="AdventureWorksModelStoreContainer" />
<!--Errors Found During Generation:
warning 6005: The data type 'hierarchyid' is currently not supported for the target .NET Framework version; the column 'DocumentNode' in table 'AdventureWorks.Production.Document' was excluded.
warning 6031: The column 'DocumentNode' on the table/view 'AdventureWorks.Production.Document' was excluded, and is a key column. The table/view has been excluded. Please fix the entity in the schema file, and uncomment.
<EntityType Name="Document">
<Property Name="DocumentLevel" Type="smallint" StoreGeneratedPattern="Computed" />
<Property Name="Title" Type="nvarchar" Nullable="false" MaxLength="50" />
<Property Name="Owner" Type="int" Nullable="false" />
<Property Name="FolderFlag" Type="bit" Nullable="false" />
<Property Name="FileName" Type="nvarchar" Nullable="false" MaxLength="400" />
<Property Name="FileExtension" Type="nvarchar" Nullable="false" MaxLength="8" />
<Property Name="Revision" Type="nchar" Nullable="false" MaxLength="5" />
<Property Name="ChangeNumber" Type="int" Nullable="false" />
<Property Name="Status" Type="tinyint" Nullable="false" />
<Property Name="DocumentSummary" Type="nvarchar(max)" />
<Property Name="Document" Type="varbinary(max)" />
<Property Name="rowguid" Type="uniqueidentifier" Nullable="false" />
<Property Name="ModifiedDate" Type="datetime" Nullable="false" />
</EntityType>-->
</Schema>
Notice this warning:
warning 6031: The column 'DocumentNode' on the table/view 'AdventureWorks.Production.Document' was excluded, and is a key column. The table/view has been excluded. Please fix the entity in the schema file, and uncomment.
Now in the AdventureWorks database the Production.Document table is referenced by Production.ProductDocument table. Since no entity for Production.Document table was created EF is unable to create the reference from Production.ProductDocument entity and hence the "Production.Document' is referenced by a relationship, but cannot be found.' error.
Since the Production.Document table cannot be really used "as is" by EF the easiest workaround is to exclude this entity when generating the model from entity - check all tables but Production.Document in the wizard and you should be good to go. EF will ignore all references to this entity since you excluded it and therefore there should be no error.
Link to a related work item on Entity Framework codeplex site

Related

Confirmation before removing all unused index in the datastore (GAE / Java)

I have a website in production, and I would like to avoid doing a big mistake...
I would like to use vaccum index command
./appengine-java-sdk/bin/appcfg.sh vacuum_indexes myapp/war
Does a backup can help restoring all indexes ?
Have you any recomendation before doing this kind of operation ?
Here is my datastore-indexes.xml:
<datastore-index kind="Event" ancestor="false" source="manual">
<property name="date_list" direction="asc" />
<property name="e_name" direction="asc" />
</datastore-index>
<datastore-index kind="U" ancestor="false" source="manual">
<property name="u02_name" direction="asc" />
<property name="u04_zona" direction="asc" />
</datastore-index>
<datastore-index kind="S" ancestor="false" source="manual">
<property name="cookie" direction="asc" />
<property name="dateCreated" direction="asc" />
</datastore-index>
Here is a screeshot of my current indexes status (in red, are the indexes I should actually use):
Thanks you,
Searching StackOverflow for this google-app-engine tag and vacuum indicates that other users often do vacuuming but do not report any data loss, so you should be all right. One question contains a good warning about an action you should avoid, namely doing update_indexes before vacuuming is complete (probably asking for eventual consistency trouble), and the upvoted answer describes a sensible recovery procedure.
To get some confidence you could use the Admin Console, Datastore Admin page to copy all your data to another app and perform all your maintenance work there first. The data volumes shown should copy fairly quickly. The copy will give you peace of mind and would also be a handy warm standby backup in case the unthinkable happens to you production data.

Foreign Key Hibernate Configuration

i'm trying to use hibernate with an existing mysql db using Eclipse.
I've succeeded mapping tables to classes and executing some query.
But i have a problem with a one to many relation.
I have the table "CARATTERISTICHE" (attributes) which is, in fact, a tree, described by a join table "VALORI" (values): "fk_child" "fk_parent".
I would like that the class Caratteristica had a field "children" with type List<Caratteristica>, where the children should be the join with CARATTERISTICA and VALORI.
My first attempt was to create a pojo Caratteristica and let eclipse generate the configurations file of hibernate.
That doesn't work because when I launch getChildren all i got is the same object (i.e. every node is his father, which is false in my db).
This is an excerpt of the generated xml:
<class name="model.Caratteristica" table="caratteristiche">
<id name="id" type="java.lang.Integer">
<column name="ID"/>
<generator class="assigned"/>
</id>
<set access="field" lazy="true" name="children"
sort="unsorted" table="valori">
<key>
<column name="id"/>
</key>
<one-to-many class="model.Caratteristica"/>
</set>
Note that if change the column key from id to fk_child he can't find fk_child in table "CARATTERISTICHE" (but it should look in VALORI, right?)
I've also tried to generate pojos from tables but it's worse..
Maybe i've got this problem hibernate- composite key configuration but.. it's my first time I use hibernate, I'm really lost!
Your data model is a bit strange.
Either you have a complete tree structure, i. e. a 1 : n relation between parents and children. Then you do not need the join table VALORI. In CARATTERISTICHE you just define a column parentId, which contains the id of the parent row. In your mapping you give this new column as the key tag: <key> <column name="parentId"/> </key>
Or you have an m : n relation between parents and children, which is defined by the join table VALORI. The tree structure can be defined as a special case with the m : n relation, but other structures are also possible. Then you must use the <many-to-many> tag (instead of one-to-many). The <many-to-many> tag contains the column attribute, which allows to specify the name of the corresponding foreign key in the other table.

How can I create an m:n relationship not using the primary key in NHibernate?

Assuming I had the following tables:
If I now set up a many-to-many relationship in NHibernate, the <key column /> attribute below will map the PublisherArticles.VersionIndependentArticleId to the primary key column of the article class (Id) instead of VersionIndependentId.
<class name="Article" table="Articles">
<id name="Id" />
<property name="VersionIndependentId" not-null="true" />
<property name="Version" not-null="true" />
<property name="Text" not-null="true" />
<set name="Publishers" table="PublisherArticles">
<key column="VersionIndependentArticleId" />
<many-to-many class="Publisher" column="PublisherId" />
</set>
</class>
Is there any way to target the Articles.VersionIndependentId column instead?
This can be done using the foreign-key property.
However, the model shown in my question does not work with NHibernate (it's actually a multi-m:n relationship: the m side of the relationship is not a unique record but a key shared by multiple records).
I'm not sure if this is bad normalization, just a limitation of my ORM or something else.

Should NHibernate generate SQL for GUID parameter without quotes?

Sql Server 2008 R2 Express. NHibernate 2.1.2.4.
I get SQL like:
SELECT customer0_.Id as Id1_0_
FROM customers customer0_
WHERE customer0_.Id=#p0;
#p0 = 11111111-1111-1111-1111-111111111111
...which returns 0 records even though there is Customer in there with that Id.
The SQL Server column datatype is UNIQUEIDENTIFIER.
<session-factory> configured as follows:
<property name="connection.provider">NHibernate.Connection.DriverConnectionProvider</property>
<property name="connection.driver_class">NHibernate.Driver.SqlClientDriver</property>
<property name="dialect">NHibernate.Dialect.MsSql2005Dialect</property>
Class's <id> configured as follows
<id name="_id" column="Id" type="Guid" access="field" unsaved-value="00000000-0000-0000-0000-000000000000" >
<generator class="guid.comb" />
</id>
Forging my way through this for the first time, w/ the help of various tuts. I've been over everything several times but no joy. Any ideas what is wrong/missing here? TYIA!
This seems correct. According to this "we must remember that a Guid is not a string even thought that's how we see them. A Guid is a 16-byte data structure." So I'm not sure why you are not getting back the results you expect, but I think the generated SQL is correct in not having the quotes.

How to work with liquibase, a concrete example

Following the quickstart on liquibase i've created a changeset (very dumb :) )
Code:
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.6"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.6
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.6.xsd">
<changeSet id="1" author="me">
<createTable tableName="first_table">
<column name="id" type="int">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="name" type="varchar(50)">
<constraints nullable="false"/>
</column>
</createTable>
<createTable tableName="new_table">
<column name="id" type="int">
<constraints primaryKey="true" nullable="false"/>
</column>
</createTable>
</changeSet>
</databaseChangeLog>
I've created a clean schema and i've launched the migrate command.
Liquibase created the database, with the support tables databasechangelog and ..lock.
Now how i can track the changes?? i've modified the changeset adding a new createTable element but when i try the command "update" liquibase tells me this
Migration Failed: Validation Failed:
1 change sets check sum
so i don't think to have understood the way to work with liquibase.
Someone may point me to the right direction??
Thanks
You should never modify a <changeSet> that was already executed. Liquibase calculates checksums for all executed changeSets and stores them in the log. It will then recalculate that checksum, compare it to the stored ones and fail the next time you run it if the checksums differ.
What you need to do instead is to add another <changeSet> and put your new createTable element in it.
QuickStart is good readin' but it is indeed quick :-) Check out the full manual, particularly its ChangeSet section.
This currently accepted answer is slightly out of date based on changes in Liquibase 2.x. In the 2.x version, Liquibase will still fail if the md5 checksum has changed for a changeset, but you can specify the runOnChange attribute if you want to be able modify it.
From the documentation:
runOnChange - Executes the change the first time it is seen and each time the change set has been changed
If it's a change to a changeset that basically has already been done, you can manually modify the database so that its md5 for that changeset matches the new one. Good for minor textual changes. Or you can delete that changeset row from your table.

Resources