I have a regular view in sql server 2008 and I`m using entity framework generate design from database approach. I already know about the keys issues for views in entity framework but that is not my solution. When I query select * from view, it brings out 3 rows which is right, but all the rows are the same and are the first row in the database view.
some help would be very appreciated.
I already know about the keys issues for views in entity framework but that is not my solution. When I query select * from view, it brings out 3 rows which is right, but all the rows are the same and are the first row in the database view.
What you describe is exactly the problem caused by incorrect key. Your three rows must have unique identification - some column or set of columns must uniquely identify each possible returned record. These columns must be set as entity key in the designer.
You can also avoid this issue by not using change tracking when loading data from the view because returned entities are read-only. You have to use MergeOption.NoTracking for that ObjectSet:
context.MyViewEntities.MergeOption = MergeOption.NoTracking;
var data = context.MyViewEntities.ToList();
Related
I have four tables, where the data should be checked for 1st table, if doesn't exist, then should check for 2nd table and goes on...
Currently following approach can be followed
1. Have union all on the tables and find out the above based on priority (Or)
2. Run each query from the application and verify and goes one by one (Or)
3. Using CASE and NOT_EXIST option to find out the rows accordingly
Problems:
1. Problem with JPA if all columns are extracted with asterisk (*) since count(c.*)
is not supported in SQL
2. Performance and unnecessary multiple query from application which needs to be
avoided in second approach
3. Makes the query has very complex and not interested to proceed
What would be the best practice to be followed on this use case?
Thanks
I have a Reporting Services Project on SQL Server 2008 R2. The point is to provide users with a data model they can use as a data source for Report Builder 3. So I have created my data source views as usual and then the Report model with no problems.
For example in my model I have a table Student which has a relationship with table Course using a CourseSK key (one to one). So I am using student.CourseSK = course.CourseSK to return to the query designer only the course.Coursename field under the Student entity (I have hidden everything else from the course table.)
The problem is that when I test it in ReportBuilder, using the query designer when I add field CourseName from entity student in the dataset, it returns two fields instead of one: CourseName and Course where course returns some values like AAAAAEAA =. I guess is some object reference but can be very confusing to the end user. How can I make this disappear?
Indeed it turns out to be a unique entity identifier used for aggregating fields with non unique values. It cannot be removed from the data set. More information here Answer
Setup:
ASP .NET Core 1 Web API
MySQL Server Community Edition 5.7
Entity Framework Core 1.0.1 with Pomelo MySQL driver (3 separate contexts, 3 separate databases).
Contexts: MainContext (maindb), Module1Context (module1db), Module2Context (module2db).
I want to execute a query that returns a list of Posts (from Module1Context), but I need to filter them by author rights (from MainContext, User entity).
So, what I am trying to do is execute a query with a JOIN clause to a different database table:
var results = await module1Ctx.Posts.FromSql("select * from `module1db`.`posts` as `p` inner join `maindb`.`users` as `u` on `p`.`AuthorId`=`u`.`Id` where <conditions here>").ToListAsync();
When this executes, I get a SQL Exception telling me that "Sequence contains more than one element". If I select p.*, it will work, but I also need to pull the user data.
The Post entity contains a reference to the user ID (public long AuthorId {get;set;}), and a fake navigation property, NotMapped, as EF couldn't join 2 databases automatically.
My first question is - would this be possible. I have a strong feeling that this will work, but that I am missing something small.
If this doesn't work, I will resort to manually executing the query, using the DbContext's Connection. If I will do this, how can I map the results into a list of Posts, including the User data?
Sequence contains more than one element
This tells you that a column appears more than once. For example if you have "ModifiedDate" column in both tables, when you do select * it will appear twice in the result set (once p.ModifiedDate and once in u.ModifiedDate.
Also additional to this, the columns returned must exactly match the model. No property which is defined in the model must be missing, that's why p.* will work.
But unless your Post model does define fields which represent values from User table, you can't return them, because it must exactly match Post and it's mapped properties.
Ad-hoc support (mapping result to an arbitrary model, like a view model) is not yet implemented in EntityFramework Core 1.0 and is a feature on the roadmap for future versions.
From the EntityFramework Core Roadmap:
Critical O/RM features
...
Raw SQL queries for non-Model types allows a raw SQL query to be used to populate types that are not part of the model (typically for denormalized view-model data).
Edit
Also from the EFCore documentation
Limitations
There are a couple of limitations to be aware of when using raw SQL queries:
SQL queries can only be used to return entity types that are part of your model. There is an enhancement on our backlog to enable returning ad-hoc types from raw SQL queries.
The SQL query must return data for all properties of the entity type.
The column names in the result set must match the column names that properties are mapped to. Note this is different from EF6.x where property/column mapping was ignored for raw SQL queries and result set column names had to match the property names.
The SQL query cannot contain related data. However, in many cases you can compose on top of the query using the Include operator to return related data (see Including related data).
I am creating a relational database for a friend and I have never used MS Access before. I am trying to make it so that the related values are shown on a table view rather than the numeric representation. For example, instead of
[1][2242], [2][443]
I would like
[1][John Smith], [2][Marilyn Monroe]
(the first value being the primary key and the second being the value linked to another table).
I have read this page about creating relationships between tables but I am still having trouble "viewing" the linked values. Microsoft Access is going to be the only way my friend is going to be viewing these results so that is why it is important to be able to see the correct values.
I have made relational databases before but only with traditional PHP and SQL and so I know what I need to do but I cannot seem to do it.
Any help would be appreciated.
Do not be misled into creating lookup values in tables. Create a form and add comboboxes to display the data from the related table, or use queries.
For example: create form to add records in multiple tables
Or
SELECT Id, Fullname FROM Table1
INNER JOIN Table2
ON Table1.FKID = Table2.ID
You can create the relationships visually in MS Access. You can also build queries in the query design window.
I have a table in the database with the following columns: ID, Name, Txt. We are using Linq To Sql to implement our DAL. In there another collegue added two extra columns so in the code the same table results: ID, Name, Txt, NameTemp, TxtTemp.
These two "fake" tables are used in different parts of the code in LINQ joins and analyzing with SQL Profiler the parsed SQL query takes the "real" columns and everything works properly.
Now I need to make an INSERT using that table, but I get an exception since also the fake columns are used in the statement.
Since I cannot add the two fake columns in the DB(since unuseful there), is there a way in which I could make an insert with Linq omitting these two columns?
I think i know where you're getting at. You should be able to add properties to a partial linq class no problem, only thing is that if you try and use a linq query against these "fake" columns, you'll get an exception when linqtosql tries to reference a column that doesn't exist in the database. I've been through this before - i wanted to be able to select columns that don't exist in the database (but do in the linq2sql dbml class) and have linq2sql translate the columns into what they really are in the database. Only problem is that there's no real easy way to do this - you can add attributes to the "fake" properties so that linq2sql thinks that NameTmp and TxtTmp are in fact Name and Txt in the sql world, only problem is that when it comes to inserting a record, the translated sql specifies the same column twice (which SQL doesn't like and throws an exception).
You can mark the column with IsDbGenerated = true - that'll let you insert records without getting the double column problem, but you can't update a record without linqtosql complaining that you can't update a computed column. I guess you can use a sproc to get around this perhaps?
I logged a bug with Microsoft a while back, which they'll never fix. The info here might help you get what you need -
http://social.msdn.microsoft.com/Forums/eu/linqtosql/thread/5691e0ad-ad67-47ea-ae2c-9432e4e4bd46
https://connect.microsoft.com/VisualStudio/feedback/details/526402/linq2sql-doesnt-like-it-when-you-wrap-column-properties-with-properties-in-an-interface
LINQ is not for inserting data, but for querying only - Language INtegrated Query. Use ADO.NET for inserting the data.
(Leaving the first part to remind my stupidity)
Check ScottGu. The classes generated are partial (mentioned here), so you can put your 2 properties into the editable part and since they won't have any mapping attribute defined, they won't be mapped nor persisted.