Can't see EF database in SQL Server Management Studio - entity-framework

I have created a database using EF code-first like there http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-code-first-walkthrough.aspx. But when I input data to db by add() and then call savechanges() I don't see new database in SQL Server databases folder and there no exceptions. Is it right? Where can I find my database and how to put it in databases folder?
I work with this code:
public class Name
{
public long NameId { get; set; }
public string FullName { get; set; }
}
public class InfoContext : DbContext
{
public DbSet<Name> Names { get; set; }
}
Then I call it:
var db = new InfoContext();
var Names = new Name
{
NameId = 1,
FullName = "test"
};
db.Names.Add(Name);
db.SaveChanges();
var test = db.Names.Find(1);//there I get correct value
I have connectionString in web.config like this:
<connectionStrings>
<add name="InfoName" providerName="System.Data.SqlClient"
connectionString="Server = .\MYINSTANCE; Initial Catalog=mydbname;" />
</connectionStrings>

Based on your comments you need to modify the web.config file in your project root (not the one in your Views folder. In there you can add a section as follows:
<connectionStrings>
<add name="EFDbContext" connectionString="Data Source = .; Initial Catalog = ITSDB; Integrated Security = true" providerName="System.Data.SqlClient"/>
</connectionStrings>
The name property of the element is the DbContext name of your Data model, so if your class is defined as:
public class SomeContext : DbContext
{
...
}
Then your config should be:
<connectionStrings>
<add name="SomeContext" connectionString="Data Source = .; Initial Catalog = ITSDB; Integrated Security = true" providerName="System.Data.SqlClient"/>
</connectionStrings>
As for the connection string, its dependent on your database.

Check the defaultConnectionFactory type in your config. I expect it is set to LocalConnectionFactory, as this seems to be default.
Change it to the following and your SQL instance will be used.
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="Data Source=.; Integrated Security=True; MultipleActiveResultSets=True;" />
</parameters>
</defaultConnectionFactory>
Your DB should appear in SQL Management Studio (SQLMS) with a name that matches the namespace and DbContext.

Or you can just put in "Data Source = " value your server's name which selected in you SQL Management Studio.
<connectionStrings>
<add name="SomeContext" connectionString="Data Source = **Server'sName**; Initial Catalog = ITSDB; Integrated Security = true" providerName="System.Data.SqlClient"/>

Related

No tables are created when using code first EF

So I'm trying to deploy a database on Azure using EF code first. I have one model:
public class User
{
[Key]
public int PersonID { get; set; }
[Required(ErrorMessage = "A first name is required.")]
[StringLength(20, MinimumLength = 2, ErrorMessage = "Your firstname needs to be atleast 2 letters long")]
[RegularExpression(#"^[a-zA-Z]*$", ErrorMessage = "Your firstname can only contain letters")]
[Display(Name = "First Name:")]
public string FirstName { get; set; }
}
DataContext-class:
public class DataContext : DbContext
{
public DataContext() : base("DataContext")
{
}
public DbSet<User> User { get; set; }
}
DataContextInitalizer-class:
public class DataContextInitializer : DropCreateDatabaseIfModelChanges<DataContext>
{
}
In the global.asax-file to initalize the datacontext:
Database.SetInitializer(new DataContextInitializer());
And then finally the string to connect to the database in the web.config:
<connectionStrings>
<add name="DataContext" connectionString="Data Source=tcp:*.database.windows.net,1433;Initial Catalog=TestApi20180311012458_db;User ID=usernamehere;Password=passwordhere" providerName="System.Data.SqlClient" />
Thou no tables are created when building the solution. I dont know where I'm missing out. Can you see whats wrong?
Thou no tables are created when building the solution. I dont know where I'm missing out. Can you see whats wrong?
Your code just to connect Azure sql in your local project. And the data initialization just uses to initialize data in table. But your tables have not created yet. If you want to create table in Azure sql, you could use Migrations.
Azure sql connection string in web.config:
<connectionStrings>
<add name="ConnectionStringName" providerName="System.Data.SqlClient" connectionString="Data Source=tcp:[databasename].database.windows.net,1433;Initial Catalog=[databasename];Integrated Security=False;User Id=[username];Password=[password];Encrypt=True;TrustServerCertificate=False;MultipleActiveResultSets=True" />
</connectionStrings>
Open Tools> Nuget Mackage Manager>Package Manager Console. Then enter the following code:
Enable-migrations
Add-migration initial
update-database
After that, you could see the tables are created in Azure Sql.
For another way, you could use publish tool to connect to Azure sql and use Code First Migrations automatically. Do not need to use code to configure manually. Open Publish settings to configure Azure sql. Or you could read this article to learn more details.

Hardcoded EF6 Connectionstring names used from dotnet core

We have an existing .NET Framework library with Entity Framework 6 and static methods like this:
public class OrderManager
{
public static OrderDTO GetOrderByOrderId(int oid)
{
var entities = new MyEntities();
....
}
}
where MyEntities have a hardcoded connectionstring name
internal partial class MyEntities : DbContext
{
public SSE3Entities() : base("name=MyEntities") {}
}
When used in a ASP.NET application, the web.config have a connectionstrings defined like this:
<connectionStrings>
<add name="MyEntities" connectionString="metadata=res://*/M..." providerName="System.Data.EntityClient" />
</connectionStrings>
But how can I reuse this library in a aspnet core application. Tried:
{
"ConnectionStrings": {
"MyEnties": "metadata=res://*/M...",
},
"Logging": {
...
}
}
I know passing the connectionstring into the OrderManager constructor is what we should have done years ago, but changing it now requiers a lot of work.
Are there any ways we can make EF read the new configurations system?
Or could we maybe write some settings to the old ConfigurationManager?
I tried dropping in a web.config without any luck
add System.Configuration.ConfigurationManager from nuget
add a file app.config
.net core applictaion is a consle app, so it find app.config first
app.config:
<appSettings>
<add key="webpages:Version" value="2.0.0.0"/>
</appSettings>
This solution will work for ASPNET Core MVC 2.0, 2.2 and 3.0 In your app root go find the appsetting.json file and modify the connection string.
{
"ConnectionStrings": {
"DefaultConnection": "Data Source=DESKTOP-3R4MR9H\\SQLSERVER3R4MR9;Initial
Catalog=ASPNetCoreMVC;Persist Security Info=True;User
ID=sa;Password=********;"
},
"Logging": {
"LogLevel": {
"Default": "Warning"
}
},
"AllowedHosts": "*"
}

Setting ConnectionTimeout when using EntityFramework

I would like to set the ConnectionTimeout to something other than the default, which is 15 seconds. I have inherited some code that uses EntityFramework and the app.config looks like this:
<configuration>
<configSections>
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=xxxxxxxxxxxxxxxx" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=.\SQLEXPRESS; Integrated Security=True; ConnectionTimeout=30; MultipleActiveResultSets=True" providerName="System.Data.SqlClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework">
<parameters>
<parameter value="Data Source=.\SQLEXPRESS; Integrated Security=True; ConnectionTimeout=30; MultipleActiveResultSets=True" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
I'm the one who added the sectino in an attempt to get things working. I can tell it's not working be setting a breakpoint at:
var adapter = (IObjectContextAdapter) this;
var objectContext = adapter.ObjectContext;
objectContext.CommandTimeout = CommandTimeoutSeconds;
int test = objectContext.Connection.ConnectionTimeout;
test is always 15. What is going on? Can someone tell me how to set ConnectionTimeout? I have tried both "ConnectionTimeout" and "Connection Timeout" I.e. no space vs. space.
Can someone help me? I'm pulling my hair out. I'm sure it's a simple fix!
Dave
Additional info. In response to comment, here is my DbContext derived class...
public class SessionDataContext : DbContext
{
// Command timeout (seconds)
private const int CommandTimeoutSeconds = 30;
/// <summary>
/// Constructor that takes db name.
/// The connection string and db itself is configured in the this project's app.config file
/// </summary>
/// <param name="dbName"></param>
public SessionDataContext(string dbName) : base(dbName)
{
Database.SetInitializer(new SessionDataContextInitializer());
// Set timeout (based on code from http://stackoverflow.com/questions/6232633/entity-framework-timeouts)
var adapter = (IObjectContextAdapter) this;
var objectContext = adapter.ObjectContext;
objectContext.CommandTimeout = CommandTimeoutSeconds;
int test = objectContext.Connection.ConnectionTimeout;
}
/// <summary>
/// Session table's records
/// </summary>
public DbSet<Session> Sessions { get; set; }
/// <summary>
/// SessionType table's records
/// </summary>
public DbSet<SessionType> SessionTypes { get; set; }
}
It was stupidity on my part that was causing the problem! I put my answer here in case anyone in the future has this problem. Everything I typed above is correct and will work fine. However, the app.config file I was looking at was in a class library (our DataAccess layer). In fact, it was not being used at all and default EntityFramework settings were being used. I'm mot sure what led me to try it, but I moved the app.config settings from the DataAccess layer app.config to the main app.config and all worked beautifully. About all I can say in my defense other than I inherited the code is that it's not clear to me to see that the values in the app.config are not being used and one does not call them or use them in one's own code. Rather, MultipleActiveResultSets and ConnectionTimeout are used by the underlying Entity Framework.

AppFabric Cmdlet - cannot connect to local cluster

I've been trying to write a simple little Cmdlet to allow me to Set/Get/Remove cache items. The problem I have is that I cannot figure out how to connect to the local cache cluster.
I've tried adding in the usual app.config stuff, but that doesn't seem to get picked up ...
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
<section name="dataCacheClient" type="Microsoft.ApplicationServer.Caching.DataCacheClientSection, Microsoft.ApplicationServer.Caching.Core, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" allowLocation="true" allowDefinition="Everywhere" />
</configSections>
<dataCacheClient>
<hosts>
<host name="localhost" cachePort="22233" />
</hosts>
</dataCacheClient>
</configuration>
I'd rather not have that config at all. So what I am really asking is what the equivalent C# code is for the following powershell...
Use-CacheCluster
From what I can gather Use-CacheCluster connect to the local cluster if no parameters are supplied
I've just done some spelunking into the AppFabric Powershell code with Reflector to see how it works under the covers. If you call Use-CacheCluster with no parameters e.g. for the local cluster, the code reads the connection string and provider name from the Registry key HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\AppFabric\V1.0\Configuration. Unfortunately, it then uses those values to build a series of classes (ClusterConfigElement, CacheAdmin and ClusterHandler) which are all marked as internal, so you can't use them to pick up the current cluster context (for want of a better word) that Powershell is working with.
To make your Cmdlet work, then, I think you need to pass in a hostname (which would be one of the servers in your cluster, and perhaps you could default this to the local machine name) and a port number (which you could default to 22233), and use those values to build a DataCacheServerEndpoint to pass to your DataCacheFactory e.g.
[Cmdlet(VerbsCommon.Set,"Value")]
public class SetValueCommand : Cmdlet
{
[Parameter]
public string Hostname { get; set; }
[Parameter]
public int PortNumber { get; set; }
[Parameter(Mandatory = true)]
public string CacheName { get; set; }
protected override void ProcessRecord()
{
base.ProcessRecord();
// Read the incoming parameters and default to the local machine and port 22233
string host = string.IsNullOrWhiteSpace(Hostname) ? Environment.MachineName : Hostname;
int port = PortNumber == 0 ? 22233 : PortNumber;
// Create an endpoint based on the parameters
DataCacheServerEndpoint endpoint = new DataCacheServerEndpoint(host, port);
// Create a config using the endpoint
DataCacheFactoryConfiguration config = new DataCacheFactoryConfiguration();
config.Servers = new List<DataCacheServerEndpoint> { endpoint };
// Create a factory using the config
DataCacheFactory factory = new DataCacheFactory(config);
// Get a reference to the cache so we can now start doing useful work...
DataCache cache = factory.GetCache(CacheName);
...
}
}
The problem is that the call:
DataCacheFactoryConfiguration config = new DataCacheFactoryConfiguration();
inside Cmdlet mothods produces an error sounding like "Cannot initialize DataCacheFactoryConfiguration".

'String cannot have zero length' error when using EF Tracing Data Provider

I am trying to incorporate 'EF Tracing Data Provider' into an existing MVC2 app using VS2010, .NET 4.0 in order to log all SQL commands. I have no interest at this time in the caching provider. I beleive I have followed all the steps listed in the blog posting. BLOG POST My project does compile without error, however when I attempt to run the project I get the following error:
'String cannot have zero length.' The error points to Extended_JCIMS_MVC2_EF_Entities.cs Line: 25
Line 25: public ExtendedJCIMS_DevEntities(string connectionString)
Line 26: :base(EntityConnectionWrapperUtils.CreateEntityConnectionWithWrappers(
I am unable to determine what is causing this error. I assume the error is referring to the connection string from the Web.Config file. It does not like the 'connectionString' variable. I'm obviously doing something worng. I would appreciate a push in the right direction.
The relevant bits are as follows:
Web.config
--------------------------------------------------------------------------
<add name="JCIMS_DevEntities"
connectionString="metadata=res://*/;provider=System.Data.SqlClient;provider connection string="Data Source=MyServer;Initial Catalog=MyDatabase;User ID=MyUser;Password=myPassWord;MultipleActiveResultSets=True""
providerName="System.Data.EntityClient"/>
<system.data>
<DbProviderFactories>
<add name="EF Tracing Data Provider" invariant="EFTracingProvider" description="Tracing Provider Wrapper"
type="EFTracingProvider.EFTracingProviderFactory, EFTracingProvider, Version=1.0.0.0, Culture=neutral, PublicKeyToken=def642f226e0e59b" />
<add name="EF Generic Provider Wrapper" invariant="EFProviderWrapper" description="Generic Provider Wrapper"
type="EFProviderWrapperToolkit.EFProviderWrapperFactory, EFProviderWrapperToolkit, Version=1.0.0.0, Culture=neutral, PublicKeyToken=def642f226e0e59b" />
</DbProviderFactories>
</system.data>
Global.ascx
-----------------------------------------------------------------------
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
RegisterRoutes(RouteTable.Routes);
//EFTracingProviderConfiguration - LOG ALL Sql commands
EFTracingProviderConfiguration.LogToFile = Server.MapPath("~/JCIMS_MVC2_EF_SQL_Logfie.txt");
}
Extended_JCIMS_MVC2_EF_Entities.cs
--------------------------------------------------------------------------
namespace JCIMS_MVC2_EF.DomainModel
{
/// <summary>
/// Partial calss that Extends the EF Datacontext Class
/// </summary>
public partial class ExtendedJCIMS_DevEntities : JCIMS_DevEntities
{
private TextWriter logOutput;
public ExtendedJCIMS_DevEntities()
: this("name=JCIMS_DevEntities")
{
}
public ExtendedJCIMS_DevEntities(string connectionString)
: base(EntityConnectionWrapperUtils.CreateEntityConnectionWithWrappers(
connectionString,
"EFTracingProvider"
))
{
}
//... and more
}
}
SearchRepository.cs
------------------------------------------------------------------
public class SQLSearchRepository : ISearchRepository
{
//Database connection
private ExtendedJCIMS_DevEntities db = new ExtendedJCIMS_DevEntities(); // tracing version
public IEnumerable<SearchResults> ListAll(string strSearch, string chkSearch)
{
return (from s in db.Schools....
// and more...
}
Appreciate any assistance anyone can give me...
Have you debugged and confirmed that the connectionString passed into the ExtendedJCIMS_DevEntities method is not null or empty? That's what the error seems to indicate.