Query designer returns an unwanted entity field - sql-server-2008

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

Related

entity framework view does not show all rows when selecting

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();

INSERT with Linq omitting some "fake" columns

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.

Entity framework result discrepancy for a database views

I have one specific view created in my DB(joins about 5-6 tables with a left join).This view is added to my edmx (entity framework 1.0) . Recently I noticed that one of the column records obtained using the edmx (linq to entities and then ToList()) got duplicated multiple times though in the database view they were different
Column-N (Expected result/ result from DB view)
---------
data1
data2
data3
data4
data5
Column-N(Actual result generated by entity framework)
---------
data1
data1
data1
data1
data1
I fired up my SQL profiler,got the query which was sent by my application to the SQL Server, ran it and it returned me the expected result.
MSDN has a similar post here and here but the moderator has not elaborated on how to solve this problem. My key happens to be a GUID
The root cause you pointed out I think
is correct, the problem is on the
application side EF mapping, as EF has
different object mapping rules with
database. when the query results have
been returned from database, the EF
will do the mapping on application
memory according to its own designed
logic.
It's important to take these logic
into account when you desingn your
view query in your database side. I
think you should do some adjustment on
your view query.
I am not sure whether you have sorted
the problem, if not please provide the
database structure related to this
issue and the view query you have
written.
Thanks Binze
Has someone encountered a similar problem before ?
The problem is in fact with the key. You have to a) have a unique identifier for each row in the view. and b) map that key accordingly in the edmx. Otherwise as your quote states, the mapping logic will see each subsequent row and figure that it can use the same object instance that it returned before
Same problem for me.
An Entity View (VReport) was generated automatically from VS2010 wizard to something like:
class VReport
Line: int (key)
Desc: string
Date: DateTime
When I retrieved the records from the database, the SQL query was formed correctly and returned the expected (and distinct) results, but the Entity Framework instead returned a lot of duplicated records.
But instead, also the Date column/field would have to partecipate in the formation the Entity KEY
So, to resolve this issue, I changed the property of the field from Entity Key: false -> true
class VReport
Line: int (key)
Desc: string
Date: DateTime (key)

LINQ InsertOnSubmit Required Fields needed for debugging

I've been using the ADO.NET Strogly-Typed DataSet model for about 2 years now for handling CRUD and stored procedure executions. This past year I built my first MVC app and I really enjoyed the ease and flexibility of LINQ. Perhaps the biggest selling point for me was that with LINQ I didn't have to create "Insert" stored procedures that would return the SCOPE_IDENTITY anymore (The auto-generated insert statements in the DataSet model were not capable of this without modification).
Currently, I'm using LINQ with ASP.NET 3.5 WebForms. My inserts are looking like this:
ProductsDataContext dc = new ProductsDataContext();
product p = new product
{
Title = "New Product",
Price = 59.99,
Archived = false
};
dc.products.InsertOnSubmit(p);
dc.SubmitChanges();
int productId = p.Id;
So, this product example is pretty basic, right, and in the future, I'll probably be adding more fields to the database such as "InStock", "Quantity", etc... The way I understand it, I will need to add those fields to the database table and then delete and re-add the tables to the LINQ to SQL Class design view in order to refresh the DataContext. Does that sound right?
The problem is that any new fields that are non-null are NOT caught by the ASP.NET build processes. For example, if I added a non-null field of "Quantity" to the database, the code above would still build. In the DataSet model, the stored procedure method would accept a certain amount of parameters and would warn me that my Insert would fail if I didn't include a quantity value. The same goes for LINQ stored procedure methods, however, to my knowledge, LINQ doesn't offer a way to auto generate the insert statements and that means I'm back to where I started.
The bottom line is if I used insert statements like the one above and I add a non-null field to my database, it would break my app in about 10-20 places and there would be no way for me to detect it. Is my only option to do a solution-side search for the keyword "products.InsertOnSubmit" and make sure the new field is getting assigned?
Is there a better way?
Thanks!
Create a NON-Null field, with a default value of whatever. This way the old rows will have a default value you know of, letting you go back and add values to the table.

Linq to SQL and Gridview Datasource

I have a question related to this one. I don't want to do a calculation (aggregation), but I need to get display values from an association. In my C# code, I can directly reference the value, because the foreign key constraint made Linq generate all the necessary wiring.
When I specify the IQueryable as the Gridview datasource property, and reference something that is not a column of the primary entity in the result set, I get an error that the column does not exist.
As a newbie to Linq, I am guessing the assignment implicitely converts the IQueryable to a list, and the associations are lost.
My question is, what is a good way to do this?
I assume that I can work around this by writing a parallel query returning an anonymous type that contains all the columns that I need for the gridview. It seems that by doing that I would hold data in memory redundantly that I already have. Can I query the in-memory data structures on the fly when assigning the data source? Or is there a more direct solution?
The gridview is supposed to display the physician's medical group associations, and the name of the association is in a lookup table.
IQueryable<Physician> ph =
from phys in db.Physicians
//from name in phys.PhysicianNames.DefaultIfEmpty()
//from lic in phys.PhysicianLicenseNums.DefaultIfEmpty()
//from addr in phys.PhysicianAddresses.DefaultIfEmpty()
//from npi in phys.PhysicianNPIs.DefaultIfEmpty()
//from assoc in phys.PhysicianMedGroups.DefaultIfEmpty()
where phys.BQID == bqid
select phys;
(source: heeroz.com)
So, based on Denis' answer, I removed all the unneeded stuff from my query. I figured that I may not be asking the right question to begin with.
Anyways, the page shows a physician's data. I want to display all medical group affiliations in a grid (and let the user insert, edit, and update affiliations). I now realize that I don't need to explicitly join in these other tables - Linq does that for me. I can access the license number, which is in a separate table, by referencing it through the chain of child associations.
I cannot reference the medical group name in the gridview, which brings me back to my question:
AffiliationGrid.DataSource = ph.First().PhysicianMedGroups;
This does not work, because med_group_print_name is not accessible for the GridView:
A field or property with the name 'med_group_print_name' was not found on the
selected data source.
Again, bear with me, if it is all too obvious that I don't understand Linq ... because I don't.
Your query seems strange. You should try to simply display
ph = from phys in db.Physicians
where phys.BQID == bqid
select phys;
in your grid. That should work.
Also, why the calls to Load()? If the DataContext is not disposed when the grid is binding, you should not need it.
If you still have issues, can you please post the error message you get, that would help...
Part 2
The problem is that you have the name is effectively not in the PhysMedGroup. You need to navigate one level down to the MedGroupLookup to access the name, since it is a property of that class.
Depending on the technology you are using (it seems to be either WinForms or Web Forms), you will need to configure your data-binding to access MedGroupLookup.med_group_print_name.