Extend DbContext class using Ef6 - c#

I need to extend the generated dbcontext in my application using EF6
namespace DAL.Entities
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Entity.Core.Objects;
using System.Linq;
public partial class ajtdevEntities : DbContext
{
public ajtdevEntities()
: base("name=ajtdevEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<ajt_adress> ajt_adress { get; set; }
}
}
When I tried to change the name of this class ajtEntities1 to ajtdevEntities
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DAL.Entities
{
public partial class ajtEntities1
{
}
}
I get this exception :
I don't understand why this exeption is appeared!!
What is the reason?
How can I fix it?

Related

Unit testing properties for Page Models in Razor Pages

I have a simple Page Model, PizzaModel:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace starting_app.Pages
{
public class PizzaModel : PageModel
{
public string Customer {get; set;}
public int Total {get; set;}
public string SomeProperty {get; set;}
public void OnGet()
{
}
}
}
I'm using the NUnit framework to test for certain properties:
using System;
using NUnit.Framework;
using blank_workspace.Pages;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;
namespace starting_app.UnitTests
{
[TestFixture]
public class Tests
{
[Test]
public void PizzaModel_Asserts_Customer_Property()
{
PizzaModel pm = new PizzaModel();
Assert.That(pm, Has.Property("Customer"));
}
}
}
Is this the right format?
I'm currently getting another type of error:
The type or namespace name ‘NUnit’ could not be found (are you missing a using directive or an assembly reference?)
But I'm trying to get my syntax correct.

Get Logged in username using a function

I have a class with the following function
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Http;
namespace Performance_Dashboard.Controllers
{
public class GetLoggedInData
{
public string ActualUser { get; internal set; }
public string GetLoggedInDataofUser()
{
ActualUser= HttpContext.Current.User.Identity.Name;
return ActualUser;
}
}
}
I would like to access or see what is being returned, how do I do that?
You said
[I set a breakpoint] ... but my program does not go to that function that means that I have to call it right?
The answer is yes. In order to hit the breakpoint you must call the method:
GetLoggedInData glid = new GetLoggedInData();
glid.GetLoggedInDataofUser();//Call the method. Now the breakpoint will be hit
An additional method you can use other than setting a break point is using the System.Diagnostics library to print to the output window.
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Diagnostics;
namespace Performance_Dashboard.Controllers
{
public class GetLoggedInData
{
public string ActualUser { get; internal set; }
public string GetLoggedInDataofUser()
{
ActualUser= HttpContext.Current.User.Identity.Name;
Debug.Write(ActualUser);
return ActualUser;
}
}
}

interface with System.IO Object cannot be completed in Xamarin.iOS project

I'm trying to make an interface like this
using System;
using System.IO;
using System.Text;
using System.Threading;
using Xamarin.Forms;
namespace Foo
{
public interface IStreamProvider
{
Stream OutputStream { get; set; }
}
}
and implement it in the iOS project like this:
using System;
using System.IO;
using Foundation;
using Foo;
[assembly: Xamarin.Forms.Dependency(typeof(StreamProvider_iOS))]
namespace Foo.iOS
{
public class StreamProvider_iOS : IStreamProvider
{
public Stream OutputStream { get; set; }
}
}
but I end up getting this error:
'StreamProvider_iOS ' does not implement interface member
'IStreamProvider.OutputStream'. 'StreamProvider_iOS.OutputStream'
cannot implement 'IStreamProvider.OutputStream' because it does not
have the matching return type of 'Stream'.
I'm thinking it's because System.IO in the base xamarin project isn't the same as System.IO in the Xamarin.iOS project, but I'm not really sure and don't know how to work around that if it's the case.

c# class how to reference in .cs file for website

I'm new in programming. I've followed the steps in the book "Pro asp.net 4 in c#2010" to create 2 classes.
Now I try to use this class on a webpage. In the .cs file I added using "DBComponent;" but Visual Studio says "The type or namespace name 'DBComponent' could not be found (are you missing a using directive or an assembly reference?)"
What did I forget?
If i try to add the compiled dll under 'references' i get the error:
The type 'DBComponent.EventDetails' in 'D:_Web\OSWeb\DBComponent\DBComponent\EventDetails.cs' conflicts with the imported type 'DBComponent.EventDetails' in 'D:_Web\OSWeb\DBComponent\DBComponent\bin\Debug\DBComponent.dll'. Using the type defined in 'D:_Web\OSWeb\DBComponent\DBComponent\EventDetails.cs'.
This is what I've done:
Create a new empty website 'OSWeb' in VS
On top of this solution I clicked right and choosed: Add > new Project > Visual C# > Windows > Class library: name: DBComponent and I stored in D:_Web\OSWeb\DBcomponent
List item
I created 2 cs files (EventDB.cs and EventDetails.cs)
my events.cs file
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DBComponent
{
public class EventDB
{
private string connStr;
public EventDB()
{...}
public EventDB(string connectionString)
{...}
public int InsertEvent(EventDetails evd)
{...}
}
}
This is my eventdetails.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DBComponent
{
public class EventDetails
{
private int eventId;
private string eventName1;
public int EventId { get { return eventId; } set { eventId = value; } }
public string EventName1 { get { return eventName1; } set { eventName1 = value; } }
public EventDetails(int eventId, string eventName1)
{
this.eventId = eventId;
this.eventName1 = eventName1;
}
public EventDetails() { }
}
}
Now I create a new webpage (this is the .cs file of this webpage) (events.aspx.cs)
using System;
using System.Data;
using System.Configuration;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBComponent; => here is the error
public partial class events : System.Web.UI.Page
{
private EventDB db = new EventDB(); => here is the same error
protected void Page_Load(object sender, EventArgs e)
{
}
}
try the following:
right click on your web project and click in properties, then click en references. remove the reference of your class, close this windows. Verify in the Bin folder from you proyect, yours references (should not be the reference has been removed.). Next, you open again the project properties and add your class library.
(before adding the reference should be sure that the class library compiles correctly.)
sorry for my english

bringing two projects together

I have a simple window application I am starting and I was given another file I would like to bring into the new project. How do I bring them both together to work as one file?
New Project
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
Other
using System;
using System.Text;
using System.IO;
using System.Net;
namespace CaptchaClient
{
public static class SampleUse
{
public static void SampleSolve()
{
}
}
public class CaptchaSolver
{
}
public enum ResponseState
{
}
}
This screenshots shows the how yo do he procedure
Right click on your project in the Solution Explorer and click Add Existing Item. Then browse to the other class file. Once imported, you might want to change its namespace to the one you are using in your project.

Categories

Resources