|
How can I define my entity sets using DbSet then? To all intents and purposes I am using Entity framework, it's calling it up in app.config, and in the references, so what can be going wrong?
|
|
|
|
|
Using CodeFirst, in your DbContext derived class:
public DbSet<myclasstype> MyTableName { get; set; }
|
|
|
|
|
Thanks Dave, But what If I'm using DB first?
|
|
|
|
|
Wait, you mentioned EF 4.0 .DLL's, correct?
Is this an old EF 4.0 project you're upgrading?? If so, see Upgrading to EF6[^].
|
|
|
|
|
I started a new project to test the find method, and I'm still getting the problem.
My app.config looks like this:
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
</entityFramework>
</configuration>
The System.Data.Entity DLL reports as being version 4.0. I downloaded a version 5.0 of this DLL, but even that reports as version 4.0
|
|
|
|
|
I've found what I've been doing wrong Dave. When I've been adding the Entity Data model when I started the project, I've been deleting the two .tt template files and setting the code generation strategy in the edmx properties to 'default'. This hasn't been generating the (correct) code to use dBSet. I've still got a lot to learn yet about this, as it's opened a few more cans of worms for me, (For example, what happened to my 'AddObject' methods?)
This highlights my original point. I'm a freelance developer, and have been for about 10 years now. I work by myself, and I have no-one to bounce ideas off, or show me my mistakes. I learnt EF by buying Julia Lerman's book and studying enough of it to get my current project up and running, but not going any further. When I worked for a software house in England, I used to dread code review day, but I can see now how useful it was.
I know I'm still missing a lot, and I would be grateful if you could point me to a good tutorial about the correct way to deal with Entity Framework, and .tt files and all.
Thanks for all your patience Dave, it's been much appreciated.
George
|
|
|
|
|
The best out there are Julia's books. I have them all and am going the CodeFirst route on my current project. I've been working on this one for the last year and will probably be doing it for another.
|
|
|
|
|
Hi there.
I'm sorry, but this is probably not the answer you would wish for, but to be honest, the best practice of MS Entity Framework is to not use it at all...
First of all, you should REALLY consider the actual need for using such technology on a project by project basis, don't just use it because it's an easy way to stay away from writing SELECT statements and Plain SQL.
EF solutions adds A LOT of complexity to a project behind the scenes. It doesn't just map one table to a class and gets done with it, but it seriously does some crazy s*** behind the curtains that you don't want to see. (probably why you don't see it in the first place).
Why do I say this? Consider the following:
For each first-time call to a EF mapped database the machinery has to go through (among other things) these steps:
1. Mapping of the views:
Mapping Views are executable representations of the transformations specified in the mapping for each entity set and association. Internally, these mapping views take the shape of "canonical query trees".
There are two types of mapping views:
Query views:
These represent the transformation necessary to go from the database schema to the conceptual schema.
Update views:
These represent the transformation necessary to go from the conceptual model to the database schema.
The process of computing these views based on the specification of the mapping is what's called "view generation".
View generation can either take place dynamically when a model is loaded, or at build time, by using "pre-generated views".
The latter are serialized in the form of Entity SQL statements to a C# file.
2. Validation of the Schema:
When views are generated, they are also validated. From a performance standpoint, the vast majority of the cost of view generation is actually the validation of the views which ensures that the connections between the entities make sense and have the correct cardinality for all the supported operations.
When a query is executed, the query is combined with the corresponding query view, and the result is run through the compiler to create a representation of the query that the backing store can understand.
For MS SQL Server it will be T-SQL SELECT statements.
The first time an update over an entity set is performed, the update view is run through a similar process to transform it into DML statements for the target DB.
If two Entities are connected via an inheritance chain or an Association, they are said to be connected. Similarly if two tables are connected via a foreign key, they are connected, and if this is not the case in your project, drop EF all together as it is overkill.
As the number of connected Entities and tables in your schemas increase, the view generation cost increases.
So, is all hope lost?
Well, you can speed things up by pre-configuring the views needed. This gives a formidable boost in performance in most cases.
If your model is in EDMX format, you should read up on T4 Template creation and use that.
Using pre-generated views moves the cost of view generation from model loading (run time) to compile time. So basically your users will rejoice and you will bang your head in the desk waiting for compiling to get done with it.
Some tips and tricks to consider:
1. EDMX models are validated at compile time, even if the model is unchanged. If your model has already been validated, you can suppress validation at compile time by setting the "Validate on Build" property to false in the properties window.
If your model isn't an edmx but is created using EDMGen, one of the outputs will be a "Views" file. This is a code file containing Entity SQL snippets for each entity set. To enable pre-generated views, you simply include the file in your project.
If you manually make edits to the schema files for the model, you will need to re-generate the views file. You can do this by running EDMGen with the /mode:ViewGeneration flag.
For further reference, see "Pre-Generate Views to Improve Query Performance"http://msdn.microsoft.com/en-us/library/bb896240.aspx.
2. If your application is only used for query scenarios, you can mark the model as read-only by adding a GenerateUpdateViews attribute on the EntityContainerMapping element in your XML mapping and setting it to false.
3. Using statically compiled query instances (CompiledQuery)
The Customer Advisory Team discusses this in their "Potential Performance Issues with Compiled LINQ Query Re-Compiles" blog post: http://blogs.msdn.com/b/appfabriccat/archive/2010/08/06/potential-performance-issues-with-compiled-linq-query-re-compiles.aspx.
4. Entity Framework 5 introduces automatic caching for LINQ to Entities. In past editions of Entity Framework creating a CompiledQuery to speed your performance was a common practice, as this would make your LINQ to Entities query cacheable. Since caching is now done automatically without the use of a CompiledQuery, we call this feature “autocompiled queries”. For more information, see:
|
|
|
|
|
here is my code:
public partial class Form1 : Form
{
string cs = @"Data Source=NADEEM-PC\SQLEXPRESS;Initial Catalog=School Managment System;Integrated Security=True";
static SqlConnection con = null;
static SqlCommand cmd = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
void Summary()
{
int Male_students;
int Female_students;
int Male_teachers;
int Female_teachers;
con = new SqlConnection(cs);
con.Open();
try
{
cmd = new SqlCommand("SELECT COUNT(*) FROM [Students Records] WHERE Gender='Male' ", con);
Male_students = int.Parse(cmd.ExecuteScalar().ToString());
cmd = new SqlCommand("SELECT COUNT(*) FROM [Students Records] WHERE Gender='Female' ", con);
Female_students = int.Parse(cmd.ExecuteScalar().ToString());
cmd = new SqlCommand("SELECT COUNT(*) FROM [Teachers Records] WHERE Gender='Male' ", con);
Male_teachers = int.Parse(cmd.ExecuteScalar().ToString());
cmd = new SqlCommand("SELECT COUNT(*) FROM [Teachers Records] WHERE Gender='Female' ", con);
Female_teachers = int.Parse(cmd.ExecuteScalar().ToString());
this.SM.Text = "" + Male_teachers.ToString() + "";
this.SF.Text = "" + Female_teachers.ToString() + "";
this.ST.Text = "" + (Male_teachers + Female_teachers).ToString() + "";
this.TM.Text = "" + Male_students.ToString() + "";
this.TF.Text = "" + Female_students.ToString() + "";
this.TT.Text = "" + (Male_students + Female_students).ToString() + "";
con.Close();
}
catch (System.Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
}
}
}
i don't get the desire result on labels named SM,SF,ST,TM,TF,TT
please help me.. .
|
|
|
|
|
Well, you don't tell us what results you expect, or actually get, so we can't really say "do this and all will be well" but frankly there is a lot of redundancy in that code.
For example:
Male_students = int.Parse(cmd.ExecuteScalar().ToString());
Male_students is an integer.
ExecuteScalar returns an integer.
So why are you converting an integer to a string, in order to parse it and convert it back to an integer again?
And:
this.SM.Text = "" + Male_teachers.ToString() + "";
You don't need this at all, and what do you think adding a empty string at the start and end of a string is actually going to do? Anything?
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
I want to show the total number of male students in label names Sm and i don't get it!!
|
|
|
|
|
Yeah, I kinda guessed that...
Don't you think what you do get, and the number you know are in the DB might be relevant? Just a little?
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
can't understand what r u saying??
|
|
|
|
|
Well, you run your code, yes?
It does something, yes?
What exactly it does do only you can see. (Remember that we can't see your screen, access your HDD, or read your mind, so we only get to work with what you tell us.)
So without knowing what it should do: "There are 15 male students, 12 female" and what it actually does do: "It shows 6 Male and 140 Female students" we don't have much to go on in the way of information.
We can't run your code and get the same results you do, because we don't have access to your database. So you need to tell us the information we can't find out any other way.
If you rang the garage and said "my car broke" you would expect them to want information like "what broke?", "where is it?", "was it the engine?", "have you run out of fuel?", and so forth, so why would you expect us to be any different?
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
It doesn't show any number there is nothind no text on the label
|
|
|
|
|
No text at all? Ok.
You also said: "I want to show the total number of male students in label names Sm and i don't get it!!"
Which is possibly wrong because your code puts Male Teachers in there:
this.SM.Text = "" + Male_teachers.ToString() + "";
That means the code above isn't executed at all - because int.ToString cannot return an empty string - it will always return a number as a string, even if that number is zero.
So, if that code isn't being executed, and since you don't say that you get a MessageBox or a fatal exception, the most likely is that you aren't calling the Summary method at all. And a quick look at your code says you aren't calling it anywhere in the code fragment you showed us. If you don't call it, you don;t get any displayed values.
Did you mean to call it from your Form1_Load event handler perhaps?
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
thank u sooooooo much u helped me alot
|
|
|
|
|
You're welcome!
(Just remember in future - the better the info you give us, the quicker we can give you a solution! )
Those who fail to learn history are doomed to repeat it. --- George Santayana (December 16, 1863 – September 26, 1952)
Those who fail to clear history are doomed to explain it. --- OriginalGriff (February 24, 1959 – ∞)
|
|
|
|
|
Tell your teacher that although it is allowed to have spaces in a table-name, and although it is supported, and even with it being merely an example and an excercise -
IT IS NOT DONE.
..and then I'd expect not to see the term "records" appended to that name. What else is going to be in there, icecream and zombies?
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi,
I was using IMultiValueConverter in my project in which I will be doing some business calculations. for this I will be using the following.
1. An DataTable
2. An XML and,
3. Some coloring logic.
I am trying to apply color for some cells in the DataGrid by performing an business calculation using the above mentioned together or any one of it. In all the cases am getting some lagging (slowness) in applying color to the deserved cell. but if I simply return color without performing an business calculation it works fine (So am sure that there is no mistake in applying or the way I integrated IMultiValueConverter) which means I was not calculating any business logics using the above mentioned things.
My Question was is there is any limitation in using IMultiValueConverter and IValueConverter in such a way that it was not overloaded with response time to the control to which it was bounded?
Note: please let me know if any one can't understand my question
_AKA_KSN_
|
|
|
|
|
_AKA_KSN_ wrote: In all the cases am getting some lagging (slowness) in applying color to the deserved cell. but if I simply return color without performing an business calculation it works fine (So am sure that there is no mistake in applying or the way I integrated IMultiValueConverter) It also proves that it's calculating the business logic that causes the slowdown.
_AKA_KSN_ wrote: My Question was is there is any limitation in using No, it's the business logic.
The UI-thread is responsible for drawing the UI, and it's a bad idea to put some BL in a presentation-layer.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
Hi,
But we found a different cause for this problem, in my DataGrid we have enabled Virtualization and some common theme. when we disable those it works fine.
which mean the Ui takes long time to load the data and renders all the values in the UI. after that we don't need Convertors any more. so we planned to fix the bug in theme and have to do some enough study in enabling virtualization.
_AKA_KSN_
|
|
|
|
|
_AKA_KSN_ wrote: But we found a different cause for this problem, in my DataGrid we have enabled Virtualization Virtual Mode does not slow down the grid; on the contrary, it's used when databinding is too slow.
_AKA_KSN_ wrote: which mean the Ui takes long time The UI-thread is there to paint a UI, not to load data or do conversions.
_AKA_KSN_ wrote: have to do some enough study in enabling virtualization. There's a walkthrough here[^].
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
|
|
|
|
|
It sounds to me like you are solving the wrong problem here. Rather than attempting to do business calculations in converters, you should create a ViewModel and perform your calculations in there. One of the overheads you have in a converter is that all elements are treated as objects, so you have unboxing to cope with.
|
|
|
|
|
hi, I use below code to create/consume and dispose sql connections.But as there are more than twenty methods/functions that I use it, same code block repeats. So, is it goog practice? Your valuable comments are welcomed.
private void islemKayitlariniYukle(string select)
{
using (SqlCeConnection sqlCEBaglantisi = new SqlCeConnection(Properties.Settings.Default.sqlBag))
using (SqlCeDataAdapter sqlAdaptor = new SqlCeDataAdapter(select, sqlCEBaglantisi))
using (DataTable dtTablo = new DataTable())
{
sqlCEBaglantisi.Open();
sqlAdaptor.Fill(dtTablo);
SqlCeCommand islemTipleriniAl = new SqlCeCommand("Select IslemTipi From IslemKaydi Group By IslemTipi", sqlCEBaglantisi);
using (SqlCeDataReader oku1 = islemTipleriniAl.ExecuteReader())
{
while (oku1.Read())
{ cmbIGIslemTipiSec.Items.Add(oku1.GetString(0).Trim()); }
}
|
|
|
|
|