Reputation: 414
I'm learning ASP.NET MVC 5 on "Visual Studio 2015 Community" as a newbie. I'm trying to add a controller with Entity Framework models.
And an error appears. I'm so confused.
Here is my code:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using System.Web;
namespace TwiApp.Models
{
public class Twilio
{
[Key]
public int id { get; set; }
public string userId { get; set; }
public string sid { get; set; }
public string authToken { get; set; }
public string fromNumber { get; set; }
public string toNumber { get; set; }
public bool status { get; set; }
[ForeignKey("userId")]
public virtual ApplicationUser twi_appuser_ref { get; set; }
}
}
My connection string to SQL Server 2014:
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=localhost;Initial Catalog=TwiAppDB;Integrated Security=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
And finally, my databasecontext file:
using Microsoft.AspNet.Identity.EntityFramework;
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Linq;
using System.Web;
using TwiApp.Models;
namespace TwiApp.DAL
{
public class DatabaseContext : IdentityDbContext<ApplicationUser>
{
public DatabaseContext() : base("DefaultConnection", throwIfV1Schema: false)
{
}
public DbSet<Twilio> Twilio_Db { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
base.OnModelCreating(modelBuilder);
}
public static DatabaseContext Create()
{
return new DatabaseContext();
}
}
}
What I have tried so far:
Any answers will be apprecited. Thank you.
Upvotes: 1
Views: 11279
Reputation: 1355
This due to the Controller scaffolding is not properly recognizing connection string in web.config file.
In Web.config, set second providerName same as first providerName and after creating controller, undo that!
i.e
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=*.*.*.*;Database=database_name;uid=sa;pwd=******;" providerName="System.Data.SqlClient" />
</connectionStrings>
now revert back to original
<connectionStrings>
<add name="DefaultConnection" connectionString="Data Source=*.*.*.*;Database=database_name;uid=sa;pwd=******;" providerName="MySql.Data.MySqlClient" />
</connectionStrings>
Upvotes: 2
Reputation: 131
you need to add opposite relation to create a controller like
ICollection<Twilio> twilio
in ApplicationUser class.
Upvotes: 1
Reputation:
I think I just had this same error:
The cause in my case was in my Part model:
public int OperationSetId { get; set; }
[ForeignKey("OperationSetId")]
public OperationSet OperationSet { get; set; }
Because I created my database using:
enable-migrations
add-migration initial
update-database
in the console, the tables in my IdentityModels.cs were not auto-generated for me(as they would have been if I ran my program).
To fix the error I inserted this line:
public DbSet<OperationSet> OperationSet { get; set; }
in my IdentityModels.cs at the very bottom of my ApplicationDbContext class
After adapted this code to your needs you will need to:
add-migration migrationTitle
update-database
in your console. Now you should be able to make the controller since your model is not referencing a non-existent table!
Upvotes: 0
Reputation: 55
In my case, the Data Context Class was missing some information. For anyone with this simple mistake:
Upvotes: 1
Reputation: 459
MVC 5 / EF 6
perhaps you can do this in the older version?
worked for me!
Upvotes: 0
Reputation: 1228
Try to update your Twilio Class like follow and EF will figure out the key and relation:
public class Twilio
{
// [Key]
public int Id { get; set; }
public string sid { get; set; }
public string authToken { get; set; }
public string fromNumber { get; set; }
public string toNumber { get; set; }
public bool status { get; set; }
// [ForeignKey("userId")]
public string userId { get; set; }
public virtual ApplicationUser ApplicationUser { get; set; }
}
Upvotes: 3