I'm new to entity framework. I've got EF 4.2, from NuGet. Now I'm hearing it would be good to get DbContext. I've got into the extension manager and tried finding it, but I see about 8 there. Which is the "righ" one? I don't know if this matters but I use both C# and VB.NET. Also, I don't know if this matters or not, but I'm using a data-first model, not a code-first or model-first models, for doing EF development.
On your EDMX design surface, right-click and pick Add Code-Generation Item:
Pick the ADO.NET DbContext Generator from the online gallery:
This adds two T4 template files (*.tt) in your solution explorer, and generates the DbContext and the entity classes for you:
And this is the resulting class derived from DbContext for your own project:
Related
Is it possible to use the 'DataTable' or 'DataSet' classes in ASP vNext Core 5.0?
When I try to use those classes, I am getting the error:
'The type or namespace name 'DataTable' could not be found'.
This question is a few months old but I see it coming up all the time so I'll post an answer.
As of beta 3, aspnetcore contains only a subset of the System.Data related members, which can be referenced in System.Data.SqlClient, and System.Data.Common. Among the more noticeable items missing from the data libraries are the following: DataTable, DataSet, IDbConnection, IDbCommand, IDbTransaction, and IDbDataParameter and IDataReader.
If you're looking to maintain some abstraction, you can reference the associated abstract classes like DbCommand, DbConnection, DbTransaction and DbDataReader. One thing to note, DbDataReader along with the SqlDbDataReader object no longer support the Close() method. Instead, you'll just call dispose.
I have not heard whether or not these members will be reintroduced in aspnetcore or not, but they don't appear to be in beta 4 either.
aspnetcore is subset of dnxcore and is considered deprecated, one should use dnxcore instead.
As for the DataTables and other related System.Data types, according to this issue they aren't going to include it soon, unfortunately.
Classes DataSet/DataTable/DataRow and everything related to them (DbCommandGenerator, DbDataAdapter etc) are not available in .NET Core 1.0 release (and in .NET Standards 1.3-1.6 specifications as well). This means that ADO.NET was reduced to minimalistic set of low-level interfaces and components (like IDbConnection, IDbCommand, IDbTransaction, DbProviderFactory). At this moment there are no confirmation that DataRow/DataTable will back in future .NET Core releases.
If you're looking for something between low-level .NET Core ADO interfaces and strongly-typed EF Core models - take a look to open-source NReco.Data library that provides alternative implementations for DbCommandGenerator and DbDataAdapter. I'm the author of this library, so you can ask me for details.
I've played around with Visual Studio Project Templates and found them useful so far.
Now I've found some material on Wizards and the IWizard interface.
The first basic steps are fairly easy to comprehend and seem very useful, but now I'd like to explore what else the IWizard interface has to offer, other than mere Text replacement. I can imagine a few use cases for the ShouldAddProjectItem method, but what - for example - if I want the user to be able to pick database tables and have the wizard generate model, mapping and CRUD-dao code (I already have an assembly for that tedious task)?
Can anyone give me links or hints on facilities of the interface?
In order to make CRUD, DAO, etc... I'll suggest you to check T4 Templates and, if you want more power.
Combine T4 with Dev Art's Entity Developer
Put it all inside a IWizard
Let me explain it a little bit more...
You'll need to develop some T4 Template integrated with Entity Developer (to apply either Entity Framework's or NHibernate's metadata) or standalone, in order to create as many CRUD-Dao-Service-DTO like classes you need.
Then, with the T4, you can create a new project, add it as long as a Entity Developer empty model, export it as project template, and make a IWizard with a custom dialog as show in the example you referenced.
In this IWizard, you ask for the Connection String, then you make text replacement with IWizard replacement and... you're done! User makes a "Update Model from Database" and the Entities are done.
I hope this hints are enough to start!
I have a solution which has mixed .net framework.
Environment(in solution) : Website 3.5 Framwork
Class Library 2.0 Framwork
another Class Library 3.5 Framwork
I have added another class library with 3.5 framwork to use LINQ to SQL
add new item linq-to-sql and then added reference to website . once I have added this additional classLibrary with linq-to-sql my intellisense is lost. Even it doesn't recongnise txtBox in the markup. I can only include stadard data types like string , int etc..(If I force using Ctrl+spacebar).
Unfortunately there are probably quite a few reasons why this could happen. According to here there is something called "Low-Impact Intellisense" which can be toggled on and off by pressing Ctrl+Alt+Space.
I want to ask How can I generate UML Class Diagram for only one project in solution ? When I generate by click Architecture -> Generate Dependency Diagram, I get diagram of solution, I have 8 projects in solution, so diagram looks terible.
I programming in c# and use VS 2010 Ulimate
Can I generate diagram only for some classes ? for example for classes in one folder ?
I don't know this tool but did you try to drag and drop manually the class you want to display into an empty class diagram ? It usually works.
If you have an MSDN subscription, you can install the Visual Studio 2010 Feature Pack 2, so that you can do this: How to: Create UML Class Diagrams from Code.
T4 has existed for several years in Visual Studio, but doesn't get a lot of attention. However, for those that know it, there seems to be some very creative and useful purposes.
I am researching some different ways that T4 is used, and I would appreciate to hear how YOU may have used it for real life scenarios. I am primarily interested in non-standard and creative uses.
Some interesting examples:
Phil Haack uses T4 to create static CSS files from .less
To Generate WPF and Silverlight Dependency Properties using T4 Templates
Note: I realize this is a discussion-oriented question, but the answers could be helpful to others. I have tagged it as subjective and also marked as "community wiki", so please allow the question to remain open. Thanks!
I am not a big fan of the stringy-ness of app.config/web.config, so I use T4 to read those files and make an AppSettings/WebSettings class that wraps the connection strings and key/values in a real class. This means that, as long as I always use AppSettings.SomeValue to reference my app.config, I get compile time checking, which is really nice.
I've used the T4 Templates within the sharp-architecture to generate everything from models to controllers to basic views.
Definitely worth checking out, even if you just want to see some advanced examples of T4 templates in action
I use T4 to:
Generate CRUD SQL Server and Oracle scripts.
Generate Data Access Layer, based on a database schema
Layer Generate Business Logic Layer, based on a database schema
Generate ASP.Net webforms, both HTML and codebehind, based on a database schema (scafolding).
It gives me a good, quick, simple, basic starting point for my projects.
And the best is I'm in control.
Here you can download an example of my templates
SubSonic 3.0 makes heavy use of T4 templates for generating your entity code.
Essentially it calls GetSchema() on your database connection and runs each table it finds through the T4 entity template. The great thing about using T4 here is that if you don't like the way it's handling your database schema, just edit the template.
I've tweaked the T4's to handle MySQL databases better for my situation, as I make use of many tinyint columns which the default T4 maps to byte types. A quick edit to the T4 gave me the type I wanted instead for my application entities.
LINQ to SQL templates for T4
http://l2st4.codeplex.com/
Templates replicating the functionality of the SQLMetal and the LINQ to SQL classes designer code-generators for both C# and VB.Net requiring just Visual Studio 2008.
Check out this podcast on T4 by Scott Hanselman talking with Kathleen Dollard.
http://www.hanselminutes.com/default.aspx?showID=170
I've used T4 to generate:
proxies (design time, for injecting/wrapping monitoring
code/logging/... in a very specific exposed api).
interface generation for a one-on-one interface/class mapping
replace reflection
code by "directly/real" calling code (maintenance advantage of
reflection code, but performance of the actual code) for instance
when allowing access to properties through an indexer, or something
in that direction.
xml generation for a java project (couldn't find a
T4-like solution for java, that is easily shared within a company, T4
is easy because it's build in and you can run it from command line)
generate enums from a master database (we generated them both for a delphi-code base and .Net code base)
T4 Templates are used heavily in the Web Service Software Factory (Service Factory).
See here for a list of more than 30 T4 Generators from the community in several areas including ASP.NET,WCF, UML, ADO.NET, .NET
http://t4-editor.tangible-engineering.com/How-Do-I-With-T4-Editor-Text-Templates.htm