Problems using Entity Framework in asp.net - c#

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?

Related

Why I can't use my classes from Models folder in Global.asax.cs?

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.

Models not recognized in namespace

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.)

ADO NET Entity Model not working when created inside app_code in webforms

im trying to upgrade to ADO.NET entity from DataSet tools cause they are outdated and I can't get it to work.
I have a BLL folder and a DAL folder in App_Code
Solution
- App_Code
- EmployeeDS.cs
- BLL
- DAL
- InventarioCiclicoDB.edmx
However when I try to do this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using InventarioCiclico.App_Code.DAL;
namespace InventarioCiclico.App_Code
{
public class EmployeeDS
{
public string Name { get; set; }
public string GetEmployees()
{
InventarioCiclicoEntities db = new InventarioCiclicoEntities();
db.
}
}
}
nothing appears but a IfNotNull method... Why does this happen? If I create the entity model on the solution root directoy it works fine, but if i do it in DAL folder it doesn't

How do I send mail using mailchimp key and id in ASP.NET C#

i want send mail (text and document) using mailchimp in asp.net c# for this. I download dll file (v3.0 API) from nuget and generate key and Id form mailchimp website.I Also Add API key in webconfig file now i can't implement code for send mail using mailchimp in cs page in asp.net c#..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MailChimp.Net;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
MailChimpManager manager = new MailChimpManager("string for API Key");
}
}
For any tool implementation/integration one should refer the original documentation of that tool.
However you can refer this github project for reference but I am not sure whether it will completely fulfill your requirement or not.
https://github.com/danesparza/MailChimp.NET

Can I declare dbcontext without Entity Framework

I'm building a tools which convert Entity Framework model object to another object. The new object should save as a file. I declare an object like below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;
namespace CreateBusinessObjectByEF
{
class BusinessObjectBuilder
{
private DbContext db;
private DbSet dbSet;
}
}
But the string DbContext and DbSet is marked by red line because namespace error. I have add the reference to System.Data.Entity, and I don't want to add the reference to Entity Framework, because I want my application can run on all the version of EF.
So who can help me? Thanks

Categories