I have a rather large DBML file and recently discovered that Visual Studio's MSLinqToSQLGenerator is generating different output than:
SqlMetal.exe All.dbml /code:All.designer.vb /namespace:LINQ2FSE /pluralize /provider:SQL2005
It seems to have dropped an arbitrary (and I think relatively small) set of associations from the generated VB code. But SQLMetal works fine. Shouldn't the output be the same?
After further research, I find that the difference seems to be associations on entities that involve properties that are also used on other associations on the same entity with a different number of columns. For example:
Entity A has columns id and name
Entity B has columns id, name and fkA (foreign key to A)
Entity C has columns id, name, fkA and fkB (nullable fkB)
Entity C has association C_A, which links fkA to A.id
it also has association C_B, which links fkA and fkB to B.fkA and B.id
The code for properties supporting C_B will not be generated by Visual Studio, but will be generated by SqlMetal.exe.
Is this kind of association allowed? Is there a reason the code is being generated differently?
It turns out (with the help of Microsoft) that SQLMetal generates different output than the IDE's MSLinqToSQLGenerator because the DBML file (generated by a tool I created) had some relationships defined where the parent could access the children, but the children did not define the parent association. Apparently you are required to define the association from the child to the parent (the foreign key relationship). If you only have an association from the parent to the children defined, and don't have the reverse association defined (or the reverse association has a different name) then the .NET source code for that association will not be generated in either direction. This is handled correctly for MSLinqToSQLGenerator, but SQLMetal does not perform as much validation, apparently, and will generate the association code anyway. Microsoft has reported this issue to development.
Related
I have a question about whether I can extract indirect relationship information from a SQL database, just using SQL.
A modelling tool I'm using is based upon an SQL database that essentially has just two tables: Elements and Relations. The table definitions are as follows
Elements:
id, type, name
Relations:
id, type, name, source, target
Elements is just a list of entities with a system generated unique ID, and Relations (also with a unique Id) uses the Elements' unique IDs to define relationships between them in the 'source' and 'target' columns.
What I'm trying to figure out is: for any two elements in the model, e.g. A and D, or A and E, in the diagram below - are they linked, and if so how - including by multiple indirect relationships through other elements?
I know I could solve this problem by writing some procedural code that recursively trawls the Relations table exhaustively checking each relationship between A and D (or E). But this is not straightforward to code.
So I wondered if anyone can point me at the best solution to this problem?
Specifically, is there an SQL-only solution to this problem or will I have to write some procedural code?
The SQL DB in question is alaSQL underpinning the Archi modelling tool. But I'm also interested if other solutions, as pumping the data into a new DBMS is not a problem.
Thanks.
I'm working on a project in ASP.NET MVC 5, with ADO.NET EF to generate data from the SQL server. I have several one-to-many relationships in the database. When I fetch data and project it to a local variable, e.g. var query = model.listFromDbChildTable.ToList(); where model.listFromDbChildTable is just entities loaded from the db. This is the lowest table in the relationship hierarchy.
So the thing is, and I am not sure why, every time I go to "locals" in VS, in debug mode, I can go deeper and deeper into my relations, for example, I am looking at a child table (department), and when I go to this child in locals, I see child has a relation with parent (above it), s I go to parent table, (I believe this is normal,) but when I'm at parent (table), again I can go to child table from there, and I am not going up one step back in the hierarchy, but so to say deeper, like a stair down all the time. So I go again to child, and child can go to parent again, every time with the same element. And I can redo this process many times?
So what is going on? Are my relations wrong or is this some normal procedure with locals in VS?
The following image shows two tables that participate in one-to-many relationship. The Course table is the dependent table because it contains the DepartmentID column that links it to the Department table.
Basically any link between two entities can be explored(Table in SQL ,class in EF)
In SQL you refer just a column in child table as key whereas in entity a property is create which has type of a class .
Like in below example, Course will have property name "Department" of type "Department" class as it depends on it (also a DepartmentId property).
you can read more in detail here on msdn..and Don't worry moving from SQL to Entity framework does give you small suprises :) .Hope it helps.
Parent Child Entity MSDN
I have two models :
Card with has_many relation to Works
I mean i have 7 static works in my database and i can choose some of them when i create a Card.
I defined a has_many relationship like this :
My problem is strange... it seems to work, but when i explore my sqlite file, i don't see any table relationship between Card and Work.
I am expecting a table with card_id and work_id.
How core data store has many relationship?
Firstly, I'd caution you from opening up the sqlite files Core Data spits out and trying to infer things from them. It's meant to be an opaque format: you're only supposed to interact with Core Data persistent stores through the Core Data APIs.
However, to answer your question: in general in database design, you wouldn't use a linking table like you describe for a one-to-many relationship, only for a many-to-many. For a one-to-many, you'd have a foreign key field in the table at the 'one' end, and have the contents of that field be the primary key of another table. For example (sorry for the random example I found on Google):
tblOrder is in a one-to-many relationship with tblOrderDetails. This is implemented by having an Order# field in the to-many side of the relationship, which is a foreign key referring to the primary key of the tblOrder table. As you can see, no linking table is needed.
A many-to-many relationship would need a linking table, but a one-to-many does not.
One final point: I note in your screenshot that you don't have an inverse relationship set up for your works or customer relationships. Core Data requires all relationships to have an inverse, otherwise your data may become corrupted. (That's somewhat a simplification, since there are other ways to work around it, but in general making inverses is easiest.)
I'm reading Pro JPA 2. The book talks begins by talking about ORM in the first few pages.
It talks about mapping a single Java class named Employee with the following instance variables - id,name,startDate, salary.
It then goes on to the issue of how this class can be represented in a relational database and suggests the following scheme.
table A: emp
id - primary key
startDate
table B: emp_sal
id - primary key in this table, which is also a foreign key referencing the 'id' column in table A.
It thus seems to suggest that persisting an Employee instance to the database would require operations on two(multiple) tables.
Should the Employee class have an instance variable 'salary' in the first place?
I think it should possibly belong to a separate class (Class salary maybe?) representing salary and thus the example doesn't seem very intuitive.
What am I missing here?
First, the author explains that there are multiples ways to represent a class in a database: sometimes the mapping of a class to a table is straightforward, sometimes you don't have a direct correspondence between attributes and columns, sometimes a single class is represented by multiples tables:
In scenario (C), the EMP table has
been split so that the salary
information is stored in a separate
EMP_SAL table. This allows the
database administrator to restrict
SELECT access on salary information to
those users who genuinely require it.
With such a mapping, even a single
store operation for the Employee class
now requires inserts or updates to two
different tables.
So even storing the data from a single class in a database can be a challenging exercise.
Then, he describes how relationships are different. At the object level model, you traverse objects via their relations. At the relational model level, you use foreign keys and joins (sometimes via a join table that doesn't even exist at the object model level).
Inheritance is another "problem" and can be "simulated" in various ways at the relational model level: you can map an entire hierarchy into a single table, you can map each concrete class to its own table, you can map each class to its own table.
In other words, there is no direct and unique correspondence between an object model and a relational model. Both rely on different paradigms and the fit is not perfect. The difference between both is known as the impedance mismatch, which is something ORM have to deal with (allowing the mapping between an object model and the many possible representations in a relation model). And this is what the whole section you're reading is about. This is also what you missed :)
I am just starting out with ADO.net Entity Framework I have mapped two tables together and receive the following error:
Error 1 Error 11010: Association End 'OperatorAccess' is not mapped. E:\Visual Studio\projects\Brandi II\Brandi II\Hospitals.edmx 390 11 Brandi II
Not sure what it is I am doing wrong.
I believe I can add some more clarity to the issue (learning as I go):
When I look at the Mapping details and look at the association, the column for operatoraccess table (from above) is blank and the drop down only includes field from the linked table.
The Entity Framework designer is terrible - I've had the same problem many times (and your problem too, Craig):
This happens when you have a many-to-one association which is improperly setup. They could very easily fix the designer to make this process simple; but instead, we have to put up with this crap.
To fix:
Click on the association, and go to the mapping details view.
Under association, click on Maps to <tablename>. Choose the table(s) which make up the many side of the relationship (ie. the table(s) which make up the *-side of the association in the designer)
Under Column, choose the table-columns which map to each entity-side Property. You get this error when one of those entries are blank.
I had the exact same problem and this is what I did to fix it.
Make sure you have an Entity Key set in your designer on the tables your making an association with. Also check that StoreGeneratedPattern is set to Identity for that Entity Key.
There's not a lot of information in your question, but, generally speaking, this means that there is an incompletely defined association. It could be that you have tried to map one table with a foreign key to another table, but have not mapped that other table. You can also get this error when you try to do table per type inheritance without carefully following the steps for implementing that feature.
Not sure of the answer, but I've just posted a similar question, which may at least help clarify the issue you are experiencing.
Defining an Entity Framework 1:1 association
I had to go back into the database itself and clarify the foreign key relationship
I had this problem in the case where I was creating both many to 0..1 and 0..1 to 0..1 associations. One entity needed associations to multiple tables, and that entity did not have foreign keys defined for those tables.
I had to do the table mappings step that is given in the accepted answer, but note that it wasn't only for many to many associations; it applied to all the types of associations I added for this entity.
In the Mapping Details view, I had to select the entity with the non-foreign key ID columns to the various tables. This is not always the "many" side of the relationship. Only there was I able to map the related entity property to the appropriate property in the original entity. Selecting the "destination" entity would not allow me to select the properties that I needed to, and the error would still exist.
So in short, I had to map using the table related to the entity that had the "non-foreign key" ID fields corresponding to the various entities' (and their tables') primary keys that I needed to associate.
Entity A
various other properties...
Id
ContactId
OrderId
etc.
Contact entity
Id
FirstName
LastName
etc.
In the mapping details, I selected Entity A's table. It then showed both ends of the association. I mapped its Entity A's Id property to its table's actual ID column (they had different names). I then mapped the Contact entity's Id field to the ContactId field on the A entity.
Simply select the many relationship table (*) from the Association>Edit Mapping & select the appropriate relationship