Thanks for taking a look at my post.
I've been working with linq to sql and have generally been happy, until i just noticed that database table names are hardcoded into the classes/dbml files-- which can't work in our environment. We need to be able to have database names completely changeable via web.config-- in one place. That's a definite requirement.
Do you know how this can be achieved with Linq To Sql? If not, does the Entity Framework behave in the same manner? Perhaps i will have to port my model.
Appreciated!
In other words, i need "Tourism_DB" in the DataContext file:
[System.Data.Linq.Mapping.DatabaseAttribute(Name="Tourism_DB")]
public partial class TourismDataContext : System.Data.Linq.DataContext
{
as well as this text in the dbml
<Database Name="Tourism_DB" Class="TourismDataContext" xmlns="http://schemas.microsoft.com/linqtosql/dbml/2007">
to NOT be used, and the value in the web.config to be used anyway.
http://weblogs.asp.net/rajbk/archive/2008/05/20/connection-strings-in-linq-to-sql-classes.aspx
note you can do this with LINQ to SQL:
Dim c As New MyClassesDataContext(ConfigurationManager.ConnectionStrings("ConnectionString1").ToString())
The method i found here worked the best http://msmvps.com/blogs/superska/archive/2009/03/13/linq-to-sql-connectionstring-in-web-config.aspx. It laid out the full process.
Related
From what I have looked at so far, I am guessing the answer to this question is "no" but I thought I would ask in the event that I am missing something.
I have looked at the new RLS (row-level security) feature of Azure SQL. One of the things that needs to be done is to set the (user) context before executing a SQL statement. Since I am also looking at entity framework, my question is whether or not I can embed or inject something like "EXECUTE AS USER = 'User1'" into the SQL that is generated by entity framework.
Is this something that is possible? I know I can execute custom SQL but I was looking to set the code up in one place and have it run the statement for all generated SQL.
may be this EF6 feature can help, and more particularly the interception interface by the IDbCommandInterceptor.
Not clear about the hability to change the generated sql but may be.
Otherwise, have you tried a context.Database.ExecuteSqlCommand("EXECUTE AS...") ?
Create a new connectionstring using the user if you need to, or, you can modify the connectionstring used by DbContext.
I apologize in advance as a I feel the answer to this question is out there, but I can't word my query properly, so I don't get much back.
Anyhow - I have a project where I mostly have a new data architecture aside from literally a couple of tables. I'd like to use EF code first. So my question is - is it possible to use code first on the majority of the project aside from a couple of classes for I which I can define stored procedures to go after tables that already exist?
Thank you
You can access the ObjectContext property of your DbContext instance in order to execute a stored procedure like so:
((IObjectContextAdapter)myDbContext).Objectcontext.ExecuteStoreQuery<ResponseType>(
"EXEC MyStoredProc #Value1, #Value2",
new SqlParameter("#Value1", value1),
new SqlParameter("#Value2", value2)
)
However, even if the tables exist, all you need is classes that match the tables - CodeFirst does not literally mean that the code has to exist first. As long as your classes match the table structure and name (or you use the fluent api or annotations to make them match), you'll be fine. Unless the stored procedures are doing something special that is hard to do in EF, I would stick to using EF consistently.
I am using EF Code First in a project with an existing Database, all works well and so far I have been creating the class for each table manually (as they have been very small) but getting to the larger tables I can only assume there must be a better way to 'import' or 'convert' the tables fields into a class somehow. Had a search around and can't really find what I'm looking for.
To clarify I want to keep it Code First.
EF Power Tools contains reverse engineer feature for this purpose.
The method - Entity Framework Code-First - looks good. But its very difficult to create all the classes for a large database.
Is there any easy way to generate the Entity Framework Code-First classes?
You can use the recently released Entity Framework Power Tools CTP1. The tool gives you the ability to reverse engineer code first, meaning the Database will be mapped to Code.
Note that all tables in your large database will be mapped. There currently is no way to choose which tables will be mapped to code. Reading through the comments, this feature will most likely be implemented in a future release.
The point of EF Code-First is that you define your domain model in code, then your user-interface and database can be easily generated from that domain model. This has a number of advantages including reducing the amount of tedious code which needs to be written, and helping to ensure your database, your domain model, and your UI match each other.
However, at some point you are going to have to write your domain model - there's no way that can be "generated" (by which I assume you mean computer-generated) as it is personal to your application.
If I've misunderstood your question, please leave a comment and I'll update my answer.
If you want to use the code-first model, but already have an existing database, you can use the Entity Framework Power Tools to generate classes.
If you're reading this after May/2012, the above tool may be out of beta!
No there is no way to generate classes for you if you are using code-first. Code first means that there is no model and no database so you can't generate classes unless you have some upfront design in any case system (UML) which will autogenerate code for you. Simply generating classes without any input about how they should look like sounds like AI from Sci-fi, doesn't it?
If you already have databse you are not using code first but database first. In such case you can have your classes generated.
Check out the link below. It's a program that will generate POCO classes from your databases. I think that's what you're looking for.
http://msormcodegen.codeplex.com/
Generate the code from the database first using database first generation and then modify the resulting code to start your code first version
As the title says, how do I view the SQL generated by Entity Framework from within my code? I'm running into an error where the EF is crashing because a field is generated by the database (a DateTime field), and I thought I set it to know that the store is generating it via StoreGeneratedPattern, but it's still crashing, so I would like to see what exactly it's trying to push up to the database.
P.S. I've only been using EF for about an hour now... Switching from L2S.
Since you don't have Sql Profiler, your best choice would be LINQPad. You can use your existing assembly.
Click Add connection -> Use a typed data context from your own assembly -> Entity framework and select your dll.
You can write queries directly against your model (or copy-paste from your code). Select the SQL 'tab' under the query window to view the generated SQL code.
You can use the Entity Framework Profiler (EFProf). It's not free, but there's a 30-day trial available. It does a lot more neat stuff besides showing you the SQL statements.
Generally, you should always use SQL Profiler to see the SQL statements that being submitted by EF into your database.
Also, I think you misunderstood about what StoreGeneratedPattern is. If you look at its possible values inside the model, you'll see that it has identity meaning that the value will be generated (by the database) when the row is inserted and will not otherwise change. The other options are Computed, which specifies that the value will be generated on inserts and updates, and None, which is the default.
So EF will not generate that DateTime field on the fly for you, you need to manually create it and then update your model from database so that EF will generate appropriate metadata to work with it at runtime.
The free AnjLab Sql Profiler will work if real SQL Profiler is not available because you're using SQL Server Express: http://anjlab.com/en/projects/opensource/sqlprofiler. It's not quite as nice as the real thing but it gets the job done well enough.
One solution would be to capture the network traffic and have a look at the data on that level. Microsoft Network Monitor does a good job of this.
Of course, that only works if you're using a separate DB server, and the connection is not encrypted.