I'm trying to do following project for learning purposes:
https://learn.microsoft.com/ru-ru/aspnet/web-forms/overview/getting-started/getting-started-with-aspnet-45-web-forms/create_the_data_access_layer
But I got stuck on the step "Updating the Global.asax file" - VS doesn't allow me to import WingtipToys.Models namespace to Global.asax.cs file.
Here's the project structure, Globa.asax.cs and 'using' error
Everything is done according to Microsoft's tutorial above.
Why can't I to import my models to global.asax.cs file?
My ProductDatabaseInitializer class:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
namespace WingtipToys.Models
{
public class ProductDatabaseInitializer : DropCreateDatabaseIfModelChanges<ProductContext>
{
protected override void Seed(ProductContext context)
{
GetCategories().ForEach(c => context.Categories.Add(c));
GetProducts().ForEach(p => context.Products.Add(p));
}....
What did really help is a re-creation of all classes in Models directory (delete all old classes and create new ones with same code) - then I started to see my classes from Global.asax.cs file.
Maybe it could help someone.
Why do I get this error:
Cannot convert lambda expression to type 'ServiceLifetime' because it is not a delegate type [TokenAuthWebApiCore.Server]
on this line of code:
public virtual void SetUpDataBase(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<SecurityContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("SecurityConnection"), sqlOptions => sqlOptions.MigrationsAssembly("TokenAuthWebApiCore.Server")));
}
This is how I use it:
public void ConfigureServices(IServiceCollection services)
{
services.AddSingleton(Configuration);
services.AddMvc();
SetUpDataBase(services);
// services.AddDbContext<SecurityContext>(options =>
// options.UseSqlServer(Configuration.GetConnectionString("SecurityConnection"), sqlOptions => sqlOptions.MigrationsAssembly("TokenAuthWebApiCore.Server")));
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
public virtual void SetUpDataBase(IServiceCollection services)
{
// Add framework services.
services.AddDbContext<SecurityContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("SecurityConnection"), sqlOptions => sqlOptions.MigrationsAssembly("TokenAuthWebApiCore.Server")));
}
This are all my using statements
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.IdentityModel.Tokens;
using System.Text;
I am thinking probably this is because the tutorial I am following a tutorial for a different version of .net core and I am using .net core version 2.2. Can you please show me how to fix this? Thank you.
I had the same error in my project. The problem was that my context class was not derived from DbContext.
public class SecurityContext : DbContext
{
}
SecurityContext is not a DbContext. If you have a DbContext called SecurityContext in your project, remove using System.Security; from your usings in your Startups.csclass or rename the DbContext to something like SecurityDbContext.
The generic type you are using when calling AddDbContext is from the System.Security namespace, but it should be your Database context.
I´ve had the same issue. I use an interface, which is implemented by my DbContext. This solved the problem for me:
services.AddDbContext<IMyDbContext, MyDbContext>(options => options.UseLazyLoadingProxies()
.UseNpgsql("MyConnectionString", opt => opt.UseNodaTime()));
Not about version compatibility error.You need to implement DbContext in your project. When you make context class, you need to inherit context class from DbContext class.
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services
.AddEntityFrameworkSqlServer()
.AddDbContext<EmployeeContext>(options =>
{
options.UseSqlServer(Configuration.GetConnectionString("EmployeeContext"));
});
I received this when I accidentally installed EntityFramework instead of Microsoft.EntityFrameworkCore in my data project.
I just had the same problem, when migrating a project to EF core. What happened is that the DB context was still defined for EF. To fix it, I changed the DbContext to include EF core, and added the DbContextOptions constructor.
Full explenation over at the aspnetcore site.
I had EntityFramework & EntityFrameworkCore installed, removed EntityFramework & it was sorted
Heey all,
I'm working on a Umbraco website. I try to reach my renderModels from my controller but, it says to me that type or namespace could not be found. So i try to add using namespace.Models;.
When i do this the namespace.controllers is reachable but when i try namespace.Models; it gives me the error the type or namespace name 'Models' does not exist in the namespace.
The folder Models does exist.
I tried searching online for it but could not find a awnser. Pls help
Edit 1
Model
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Umbraco.Core.Models;
using Umbraco.Web;
using Umbraco.Web.Models;
using System.Globalization;
namespace MyNamespace.Models
{
public class HomeRenderModel : BaseRenderModel
{
public HomeRenderModel()
{
//
// TODO: Add constructor logic here
//
}
}
}
Controller
using System.Web.Mvc;
using Umbraco.Web.Models;
using Umbraco.Web.Mvc;
using MyNamespace.Models;
namespace MyNamespace.Controllers
{
public class HomeController : RenderMvcController
{
public override ActionResult Index(RenderModel model)
{
var rendermodel = new HomeRenderModel(model.Content, model.CurrentCulture);
return CurrentTemplate(rendermodel);
}
}
}
Hope this helps, but basically I did the tutorial that mentions the model and controller etc. out there and had the same problem. I figured out that I had created the model folder containing the classes in the wrong place. You may have the same simple problem here.
Minimize all your folders in the project explorer, and then expand to see where you placed them. Your sub folders need to be in the project (in the 'folder' with the little globe icon, not above it with the solution properties.)
I've created an ASP.Net web application using VS 2017. Now I'm trying to use Entity Framework to connect to a database.
I've added an ADO.NET Entity Data Model named TestDBModel.edmx
I've added a connection to my database file Test.mdf and named the entity connection settings TestEntities
I've selected the tables I want to use and named the model namespace TestModel
After a few seconds the designer shows the diagram with all the tables I've selected before
Now I want to get access to the tables using for example this code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using TestModel;
namespace Test
{
public partial class TestForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
TestEntities entities = new TestEntities();
GridView1.DataSource = entities.Departments;
GridView1.DataBind();
}
}
}
This is the way the manual (Beginning ASP.NET 4.5 in C#) told me to do.
But the line using TestModel; creates an error because he can't find the namespace TestModel. Thereby the next lines also creates errors because of the missing namespace.
If I look at the properties of the designer, it shows me the namespace TestModel and also the entity name TestEntities.
Can you tell me what I'm doing wrong?
I have created a context of Entity Framework like below:
using System;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace UniversityApp.Models
{
public class ProfDb : DbContext
{
public DbSet<Professor> Professors { get; set; }
public DbSet<Student> Students { get; set; }
}
}
I am using VS 2015 Professional. Then I go to View=>Server Explorer=> Add Connection=> Microsoft Sql Server => In Data Source I choose (LocalDb)\MSSQLLocalDB , and when I go to select a database name I do not find UniversityApp.Models.ProfDb as an option.
I have also created a new object of ProfDb in one of my controller but still not working. Should anyone tell me why mapping is not working in my application and what I should do?
Try this,
Go to Web config, search for
<entity framework>
Add this
<contexts>
<context type="UniversityApp.Models.ProfDb , UniversityApp">
<databaseInitializer type="Your SEED , UniversityApp" />
</context>