I am very new to SP.NET MVC, so excuse me for missing basic stuff.
I have created a website working with a database (Microsoft SQL Server) and I'm using Entity Framework package to manage it.
I have a DatabaseManager class as following:
Public class DatabaseManager : DbContext
{
Public DbSet<Connection> Connections { get; set; }
.
.
.
}
My application listens for syslog connections and then updates the Connections member accordingly:
Db.connections.Add(new Connection(SourceIp, DestinationIp))
db.SaveChanges();
Anyway, however weird it may seem, when I add a connection to the DbContext two connection are added (both identical) instead of one.
What might be my problem?
If you have registered event listener twice or included the registering file (may be JS file) twice, this issue would come. You can any time check at Firebug with list of events attached..
Related
We've been using EF6 for a while now to connect with an Azure database. For this database we use the ExecutionStrategy specific for Azure connection to have a more resilient connection:
public class MyDbConfiguration : DbConfiguration
{
public MyDbConfiguration()
{
SetExecutionStrategy("System.Data.SqlClient", () => new SqlAzureExecutionStrategy());
}
}
See the microsoft article about connection resilience for more info
Recently however we've added a connection to a different database which resides on a MSSQL database server where we want to use the default execution strategy. Since this runs in the same app domain, we run into a problem:
The default DbConfiguration instance was used by the Entity Framework before the 'MyDbConfiguration' type was discovered. An instance of 'MyDbConfiguration' must be set at application start before using any Entity Framework features or must be registered in the application's config file. See http://go.microsoft.com/fwlink/?LinkId=260883 for more information."
Reviewing the article linked in the error I see the following statement:
Create only one DbConfiguration class for your application. This class specifies app-domain wide settings.
I've tried solutions from several related questions to this, but I keep running in the same problem. The things I've tried basically come down to setting the custom DbConfiguration in different ways, through code, attribute or config file.
I think the solution is to set the execution strategy without a custom DbConfiguration, but I'm not really sure it is, and how I should do this.
I have two services running on Azure. An Event Hub and a CosmosDB/DocumentDB database.
My goal is two wire the two with a WebApp service so everything that gets on the Event Hub is consumed and properly stored on the database.
I went through the Quick Starts and the Tutorials of both Event Hubs and CosmosDB and I cannot figure out a way of wiring the two.
I know how to establish a connection with DocumentDB, I know how to consume the data of an Event Hub, but I can't manage to do both. Here is the deal:
To create an Event Hub Processor Host I've only found the following constructor
public EventProcessorHost(string eventHubPath, string consumerGroupName, string eventHubConnectionString, string storageConnectionString, string leaseContainerName)
with storageConnectionString being a combination of two strings called StorageAccountName and StorageAccountKey on the official tutorial.
Well that's actually my problem, Storage Account is another service available on Azure. I've created one for testing purposes and it works just fine but, I need to store everything on a DocumentDB CosmosDB database.
I am not excluding the possibility that going through a Storage Account is required, but if that's so, could you tell me why?
Thank you very much.
The Event Hub Processor requires connection information for Azure Storage for lease management and check-pointing purposes. Practically this means that if you have multiple instances of your processor running together all the hard work of figuring out who is reading from which Event Hub partitions is completely managed for you.
The EventProcessor class is extremely generic. You just subclass it and then implement public override Task HandleEventData(IEnumerable<EventData> data). Inside this method you're free to write EventData into Cosmos or do anything else your heart desires with the messages coming off of the Event Hub. For example:
class CosmosEventHubProcessor : EventProcessor
{
private DocumentClient _documentClient;
public CosmosEventHubProcessor()
{
// Initialize DocumentClient
}
public override Task HandleEventData(IEnumerable<EventData> data)
{
// Write data to Cosmos using DocumentClient
}
}
I have an ASP.NET project that I am working on from a Udemy class. I am also trying to familiarize myself with Git Hub so after an evening of working on the project, I will push it to the remote repository. When I build and run the application on my machine at home it runs perfectly. However, when I use my computer at work to clone the project from Git Hub, and run it, the project will build, however when I press a link that leads to a page that requires a database I get an error with my controller class '
The model backing the 'ApplicationDbContext' context has changed since
the database was created. Consider using Code First Migrations to
update the database (http://go.microsoft.com/fwlink/?LinkId=238269).
So I use the NuGet package manager to 'Update-Database' and receive the following error.
Cannot find the object "dbo.Movies" because it does not exist or you
do not have permissions.
If I download the zip file from Git Hub I receive the same errors, however I put my project on google drive and downloaded it to my work computer and it works fine. So I'm not sure what the problem would be, why my Movies database is not being built. Perhaps I did something incorrectly while pushing the project from my computer to Git Hub? I checked SQL Server object Explorer, my other database tables are there(dbo.Customers), just not dbo.Movies. Here is the code to my DbContext class:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
// add customers table to database
public DbSet<Customer> Customers { get; set; }
// add movies table to database
public DbSet<Movie> Movies { get; set; }
public ApplicationDbContext()
: base("DefaultConnection", throwIfV1Schema: false)
{
}
public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}
}
I did do a search and tried turning off the DB initializer, to see what would happen, but dbo.Movies still comes up missing. The files from my Google drive download and the Github clone seem to match up. I'm just not sure what direction to head from here. Thank you for any help provided.
Answering the question just to close it down. Project was missing the .mdf file because, as mason said, 'Database files normally aren't committed to source code control.' Something I'll keep in mind from now on. I appreciate the help mason!
I am new to MVC and Entity Framework . I am using code first approach. So far I came to know that database is created automatically when the application run for the first time . I have created Model classes and wanted to populate data in the table through seed method . The sample application is working fine on browser but the database is not present in the SQL server.
**************************CONTEXT**************************
public class userInformationContext:DbContext
{
public userInformationContextClass()
{
}
public DbSet<User> users { get; set; }
}
I have been looking for the issue over the internet but didn't got any solution.I am following this Link! . Any help will be appreciated.
Just add correct connection string in web config. It will create the sql database on that server
<add name="BloggingCompactDatabase"
providerName="System.Data.SqlClient"
connectionString="Server=myServerName\myInstanceName;Database=myDataBase;User Id=myUsername;
Password=myPassword;"/>
I have an MVC4 website and I am trying to integrate some Mongodb functionality (just experimenting for now). I am using this mongodb project as I am also trying to deploy to Azure, this project provides connection utilities for connecting to the mongodb worker role. I have a MovieController with the following constructor:
public class MovieController : Controller
{
MongoMovieHelper _movies;
public MovieController()
{
_movies = new MongoMovieHelper();
}
}
This in turn calls the class:
public class MongoMovieHelper
{
public MongoCollection<Movie> Collection { get; private set; }
public MongoMovieHelper()
{
MongoServerSettings serverSettings = ConnectionUtilities.GetConnectionSettings();
MongoServer server = MongoServer.Create(serverSettings);
MongoDatabase db = server["movies"];
Collection = db.GetCollection<Movie>(typeof(Movie).Name.ToLower());
}
...
When trying to load any page from the Movie controller I get an internal server error in the Chrome debugger.
Failed to load resource: the server responded with a status of 500 (Internal Server Error)
So I tried debugging in visual studio. The controller constructor gets hit, but any breakpoints within MongoMovieHelper do not, and trying to Step into just hits the controller constructor again. The stack trace displays the following error:
Could not load file or assembly 'MongoDB.Driver, Version=1.4.2.4500,
Culture=neutral, PublicKeyToken=f686731cfb9cc103' or one of its
dependencies. The located assembly's manifest definition does not
match the assembly reference. (Exception from HRESULT: 0x80131040)
Previously I had another class which makes the Mongodb connection so Controller calls MongoMovieHelper which calls MongoDBHelper. In this case, both the controller and MongoMovieHelper were being called, but MongoDBHelper was not, so I believe the problem lies within the mongodb connection.
This is probably some rookie mistake but I just can't figure out what the problem might be, any ideas?
I tried to recreate your problem using the latest version of the official mongodb c# driver but I could not.
I am using the latest official driver available on NuGet:
I changed your code to use non-obsolete methods to get the server object:
public MongoMovieHelper()
{
var client = new MongoClient(MongoUrl.Create("mongodb://localhost:27017"));
var server = client.GetServer();
var db = server["movies"];
Collection = db.GetCollection<Movie>(typeof (Movie).Name.ToLower());
}
So if you are not using the official driver then please use it:
get using the following command:
Install-Package mongocsharpdriver
Then try and change your code to look something like mine and then see if it works.
The error you are getting suggests you are either using an old version of the official driver, or you aren't using the official driver at all.
Note: uninstall the old driver, either through the nuget console (/ui), alternatively remove the binary ref and clean your project ensuring that you don't have any bin dll left hanging around.