Set Entity Framework connection string at runtime - c#

I have generated entity model from AdventureWorks database; now I want to delete the connection string in app.config and set it at runtime. In the Model1.Context.cs file I have chnaged the constructor to
public AdventureWorksEntities(string str)
: base("name=AdventureWorksEntities")
{
this.Database.Connection.ConnectionString = str;
}
and in the program.cs file
EntityConnectionStringBuilder ecsb = new EntityConnectionStringBuilder();
ecsb.Metadata = #"res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl";
ecsb.Provider = #"System.Data.SqlClient";
ecsb.ProviderConnectionString =
#"data source=.\sqlexpress;initial catalog=AdventureWorks;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework";
using (var ent = new AdventureWorksEntities(ecsb.ConnectionString))
{
Console.WriteLine(ent.Database.Connection.ConnectionString);
var add = ent.Addresses;
foreach (var ad in add)
{
Console.WriteLine(ad.City);
}
}
Console.ReadKey();
Now it says metadata keyword not found. How to set connectionstring for entityframework at runtime?

This is an example using standard .aspx login information to set the UserID and Password information in the connection string. No connection string settings are stored in the web.config or app.config file.
Modify the Model.Designer.cs page as follows:
public partial class Entities : ObjectContext
{
#region Constructors
public static string getConStrSQL(string UID,string PWD)
{
string connectionString = new System.Data.EntityClient.EntityConnectionStringBuilder
{
Metadata = "res://*",
Provider = "System.Data.SqlClient",
ProviderConnectionString = new System.Data.SqlClient.SqlConnectionStringBuilder
{
InitialCatalog = "your_database_name",
DataSource = "your_server",
IntegratedSecurity = false,
UserID = UID,
Password = PWD,
}.ConnectionString
}.ConnectionString;
return connectionString;
}
/// <summary>
/// Initialize a new Entities object.
/// </summary>
public Entities(string UID,string PWD)
: base(getConStrSQL(UID,PWD), "Entities")
{
this.ContextOptions.LazyLoadingEnabled = true;
OnContextCreated();
}
......
Then in your code behind page:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Mvc;
using System.Web.Security;
public partial class views_html_form : System.Web.UI.Page
{
public void Page_Load()
{
if (currentUser() == null)
{
HttpContext.Current.Response.Redirect("~/login.aspx");
}
}
public static MembershipUser currentUser()
{
MembershipUser currentUser = Membership.GetUser();
return currentUser;
}
public static string UID()
{
string UID = currentUser().UserName;
return UID;
}
public static string PWD()
{
string PWD = currentUser().GetPassword();
return PWD;
}
public static void SelectRecord()
{
YourModel.Entities db = new YourModel.Entities(UID(), PWD());
var query = from rows in db.Table_Name orderby rows.ID select rows;
.....
That's it. No messing around with .config files. Alternatively you could send a database name, for example, as a parameter in the same way.

I'd go with something like:
public AdventureWorksEntities(string server, string databaseName, string user, string password)
:base(new System.Data.EntityClient.EntityConnectionStringBuilder
{
Metadata = "res://*",
Provider = "System.Data.SqlClient",
ProviderConnectionString = new System.Data.SqlClient.SqlConnectionStringBuilder
{
InitialCatalog = databaseName,
DataSource = server,
IntegratedSecurity = false,
UserID = user,
Password = password,
}.ConnectionString
}.ConnectionString)
{
}

Related

How to insert data into database using json object in console application

I'm trying to insert data into a SQL Server database using a console application. Data to be inserted is in a json object (it is the web api post method request body). Can anyone please tell me how to use json object to insert into a SQL Server database?
Here is the code:
namespace CreateEntityConsole
{
class Entity
{
string domain = "DESKTOP-I4VK2LV";
string userName = "AP-502";
string password = "pass";
string appID = "bbb";
string locale = "en-US";
string contenttype = string.Empty;
// Create ENTITY
public string CreateEntity()
{
string URI = "http://localhost:13490/agilepointserver/extension/createentity";
string JsonRequestData = "{\"EntityName\":[\"AccountContact\":[\"PropertiesJSON\":[\"AName\": \"chaitratest2\",\"region\": \"India\"]]]}";
HttpOperations ops = new HttpOperations(domain, this.userName, password, appID, locale);
// HttpOperations ops = new HttpOperations(this.userName, password, appID, locale);
return ops.InsertEntity(URI, JsonRequestData);
}
public void InsertIntoDB(string JsonRequestData)
{
using (SqlConnection sqlCon = new SqlConnection())
{
sqlCon.ConnectionString = "server=DESKTOP-I4VK2LV;Integrated Security=True;database=Entity";
sqlCon.Open();
}
}
}
}
Use Entity framework to save json objects instead of using sqlConnection
Your can than save this to a new Ef Dbcontext(), in your case consider to deserializing json string into simple poco objects take a look at Deserializing JSON data to C# using JSON.NET
namespace CreateEntityConsole
{
public class Entity
{
private DbContext context;
public Entity()
{
context = new DbContext();
}
public void InsertIntoDB(Object JsonRequestData)
{
context.Entity.Add(JsonRequestData);
context.SaveChanges();
}
//Other CRUD stuff
}
}
Note that code first method is used, separating your the model from data access code is good practice
public class Entity {
string domain = "DESKTOP-I4VK2LV";
string userName = "AP-502";
string password = "pass";
}
public class DataAccessLayer{
DbContext context=new DbContext();
public void InsertIntoDB(Object JsonRequestData)
{
//Save json object to Entity poco
context.Entity.Add(JsonRequestData);
context.SaveChanges();
}
//Other CRUD stuff
}

Override DbContext EF6

I'm trying to understand how to dynamically create the connection string for my DbContext, but my application says it has no connection string in the app.config (and that's correct because I don't want to use it in the app.config or web.config). This is what I have:
In my solution I have a project called InterfaceApp. It is a ASP.NET MVC 5 application. When I put my connection string in the web.config all seems to be working fine.
In my solution I have an other project called InterfaceApp.Connector.Erp1. Here I want to connect to an ERP application and fetch some items. So in my repository I have:
namespace InterfaceApp.Connector.Erp1.Repository
{
internal class ItemRepository : IItemRepository
{
public IEnumerable<Item> Items
{
get
{
List<Item> items = new List<Item>();
using (Models.Entities context = new Models.Entities())
{
var itemList = context.Items.ToList();
foreach(var item in itemList)
{
items.Add(new Item() { Id = item.ID, Description = item.Description, ItemCode = item.ItemCode });
}
}
return items.ToList();
}
}
}
}
I've created a partial class to connect to the database:
namespace InterfaceApp.Connector.Erp1.Models
{
public partial class Entities
{
public Entities(string connectionString)
: base(ConnectionString())
{
}
private static string ConnectionString()
{
SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder
{
DataSource = "MyServer", //When this works it will be dynamic
InitialCatalog = "XXX", //When this works it will be dynamic
PersistSecurityInfo = true,
IntegratedSecurity = true,
MultipleActiveResultSets = true,
};
var entityConnectionStringBuilder = new EntityConnectionStringBuilder
{
Provider = "System.Data.SqlClient",
Metadata = "res://*/Models.Erp1Model.csdl|res://*/Models.Erp1Model.ssdl|res://*/Erp1Model.msl",
ProviderConnectionString = sqlBuilder.ConnectionString
};
return entityConnectionStringBuilder.ConnectionString;
}
}
}
The Context class that is auto-generated by EF6 (Db First) looks like this:
namespace InterfaceApp.Connector.Erp1.Models
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class Entities : DbContext
{
public Entities()
: base("name=Entities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<Items> Items { get; set; }
}
}
When I run my application, the debugger stops at the auto-generated class, but not at my partial class. Because it cannot find the connection string Entities in my app.config and web.config it generates an error saying that the connection string is not found in the application config file. What am I doing wrong?
When you are calling the DbContext, you're calling the empty constructor (new Models.Entities()). Thus, it will call the auto-generated DbContext. If you want to call your partial class, you need to call it explicitly with the parameter.
Remember when you create a partial class, the compiler merges them, so you have this when compiled :
public partial class Entities : DbContext
{
public Entities()
: base("name=Entities")
{
}
public Entities(string connectionString)
: base(ConnectionString())
{
}
private static string ConnectionString()
{
SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder
{
DataSource = "MyServer", //When this works it will be dynamic
InitialCatalog = "XXX", //When this works it will be dynamic
PersistSecurityInfo = true,
IntegratedSecurity = true,
MultipleActiveResultSets = true,
};
var entityConnectionStringBuilder = new EntityConnectionStringBuilder
{
Provider = "System.Data.SqlClient",
Metadata = "res://*/Models.Erp1Model.csdl|res://*/Models.Erp1Model.ssdl|res://*/Erp1Model.msl",
ProviderConnectionString = sqlBuilder.ConnectionString
};
return entityConnectionStringBuilder.ConnectionString;
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<Items> Items { get; set; }
}
What you probably need a a method to create your DbContext and call it instead of calling a new DbContext.
public static Entities Create()
{
return new Entities(ConnectionString());
}
Then you can use it this way :
using (var context = Entities.Create())
{
//...
}

EntityFramework 6 Code-based configuration of connection string for database-first

I'm attempting to make an existing application work without an app.config (it is required due to a very specific environment). Problem is that it's heavily relying on EntityFramework 6 to work with an SQL-Server.
I'm trying to use a code-based configuration, but I can't figure out how to provide a correct connection string through my configuration class.
I made a configuration class:
public class MyConfiguration : DbConfiguration
{
public MyConfiguration()
{
SetDefaultConnectionFactory(new MyConnectionFactory());
SetProviderServices("System.Data.SqlClient", System.Data.Entity.SqlServer.SqlProviderServices.Instance);
}
}
Then provided it to my DbContext (Generated by EF automatically from bd):
[DbConfigurationType(typeof(MyConfiguration))]
public partial class TestModelEntities
{
}
With a custom connection factory:
public class MyConnectionFactory : IDbConnectionFactory
{
public DbConnection CreateConnection(string nameOrConnectionString)
{
var newConnStringBuilder = new SqlConnectionStringBuilder
{
UserID = "user",
Password = "pass",
InitialCatalog = "databaseName",
DataSource = "serverName"
};
var entityConnectionBuilder = new EntityConnectionStringBuilder
{
Provider = "System.Data.SqlClient",
ProviderConnectionString = newConnStringBuilder.ToString(),
Metadata = #"res://*/TestModel.csdl|
res://*/TestModel.ssdl|
res://*/TestModel.msl"
};
var newDbConnect = new EntityConnection(entityConnectionBuilder.ToString());
return newDbConnect;
}
}
However. When I test it, I get an UnintentionalCodeFirstException. Why? What am I missing?
You should provide connection string to your context via :base(connectionString). Create a class as below:
public class ConnectionStringBuilder
{
public static string Construct()
{
var newConnStringBuilder = new SqlConnectionStringBuilder
{
UserID = "user",
Password = "pass",
InitialCatalog = "databaseName",
DataSource = "serverName"
};
var entityConnectionBuilder = new EntityConnectionStringBuilder
{
Provider = "System.Data.SqlClient",
ProviderConnectionString = newConnStringBuilder.ToString(),
Metadata = #"res://*/TestModel.csdl|
res://*/TestModel.ssdl|
res://*/TestModel.msl"
};
return entityConnectionBuilder.ToString();
}
}
Then modify your Context constructor to look like this:
public DbContext()
: base(ConnectionStringBuilder.Construct())
{
}
It should work fine now. (source)

Why is my Entity Framework connection string not working

I am trying to connect to a database without using App.Config but i keep getting the following error:
An unhandled exception of type 'System.Data.Entity.Core.EntityException' occurred in EntityFramework.dll
Additional information: The underlying provider failed on ConnectionString.
I can't see where I've gone wrong so i thought i'd ask here.
namespace MyNameSpace
{
using System;
using System.Data.Entity;
using System.Data.Entity.Core.EntityClient;
using System.Data.Entity.Infrastructure;
public partial class Entities : DbContext
{
public Entities()
: base(entityString.ToString())
{
}
public static EntityConnectionStringBuilder entityString = new EntityConnectionStringBuilder()
{
Provider = "System.Data.SqlServerCe.4.0",
Metadata = "res://*/RS.csdl|res://*/RS.ssdl|res://*/RS.msl",
ProviderConnectionString = #"C:\RestOfPath\database.sdf;Password=3476dfg423434563466e85rcsd"
};
}
}
Thank you in advance for your help.
The problem is that you are passing your sdf file directly on your connection string. Try changing:
ProviderConnectionString = #"C:\RestOfPath\database.sdf;Password=3476dfg423434563466e85rcsd"
To:
ProviderConnectionString = #"Data Source=C:\RestOfPath\database.sdf;Password=3476dfg423434563466e85rcsd"
Or better yet, use a SqlCeConnectionStringBuilder to construct this property:
var connectionStringBuilder = new SqlCeConnectionStringBuilder();
connectionStringBuilder.DataSource = #"C:\RestOfPath\database.sdf";
connectionStringBuilder.Password = "3476dfg423434563466e85rcsd";
EFConnectionBuilder.ProviderConnectionString = connectionStringBuilder.ToString(),
Try this : make this parameterise
public Entities(string connString)
: base(connString)
{
}
and pass string connection string when creating object of Context class.
public class TestController : Controller
{
Entity _context = new Entity("data source=Dev-4;initial catalog=test1;
integrated security=True;MultipleActiveResultSets=True;
App=EntityFramework");
}
Try this : here you din't need to pass connection string again and again -->
public Model1()
: base(connString)
{
}
public static string connString = "data source=tesst;initial catalog=test1;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework";
Use This method when using Database First Model of Entity Framework :
public test1Entities()
: base(nameOrConnectionString: ConnectionString())
{
}
private static string ConnectionString()
{
SqlConnectionStringBuilder sqlBuilder = new SqlConnectionStringBuilder();
sqlBuilder.DataSource = "DEV-4";
sqlBuilder.InitialCatalog = "test1";
sqlBuilder.PersistSecurityInfo = true;
sqlBuilder.IntegratedSecurity = true;
sqlBuilder.MultipleActiveResultSets = true;
EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
entityBuilder.ProviderConnectionString = sqlBuilder.ToString();
entityBuilder.Metadata = "res://*/";
entityBuilder.Provider = "System.Data.SqlClient";
return entityBuilder.ToString();
}

How to update UserProfile Properties on Sharepoint Online 2013 (O365)

I've been trying to update a user profile properties using c# on Sharepoint Online 2013.
I can't find how to do it, can someone help me?
Here is what i have to do:
I have a lot of custom properties on User Profile, and i need to edit it on an Provider-Hosted app.
I'm using PersonProperties and PeopleManager to get the data, so how to update that?
I appreciate your help!
This will probably be of some help
Using the UserProfileService, this class should help with your issue
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using Microsoft.SharePoint.Client;
using O365ProfileUpdate.UserProfileServiceRef;
public class O365Helper
{
private readonly UserProfileService _userProfileService;
private readonly Uri _targetAdminSite;
public O365Helper(UserProfileService userProfileService, Uri targetAdminSite, string adminUsername,
string adminPassword)
{
_userProfileService = userProfileService;
_targetAdminSite = targetAdminSite;
var authenticated = AuthenticateAdministrator(adminUsername, adminPassword);
if (!authenticated)
throw new UnauthorizedAccessException("Unable to authenticate administrator");
}
public PropertyData GetProfileProperty(string login, string propertyName)
{
var memLogin = GetMembershipLogin(login);
return _userProfileService.GetUserPropertyByAccountName(memLogin, propertyName);
}
public bool UpdateProfileProperty(string login, string key, string value)
{
try
{
var valueData = new ValueData {Value = value};
var newdata = new PropertyData[1];
newdata[0] = new PropertyData {Name = key, Values = new ValueData[1]};
newdata[0].Values[0] = valueData;
newdata[0].IsValueChanged = true;
var memLogin = GetMembershipLogin(login);
_userProfileService.ModifyUserPropertyByAccountName(memLogin, newdata);
}
catch
{
return false;
}
return true;
}
private bool AuthenticateAdministrator(string login, string password)
{
try
{
var securePassword = new SecureString();
foreach (char c in password)
{
securePassword.AppendChar(c);
}
var onlineCredentials = new SharePointOnlineCredentials(login, securePassword);
string authCookieValue = onlineCredentials.GetAuthenticationCookie(_targetAdminSite);
var cookieVal = authCookieValue.TrimStart("SPOIDCRL=".ToCharArray());
_userProfileService.CookieContainer = new CookieContainer();
_userProfileService.CookieContainer.Add(new Cookie(
"FedAuth",
cookieVal,
String.Empty,
_targetAdminSite.Authority));
}
catch
{
return false;
}
return true;
}
private string GetMembershipLogin(string login)
{
return "i:0#.f|membership|" + login;
}
}
adminUsername and adminPassword are the credentials for a user with administrative privileges (so, probably you) in your instance
The UserProfileService can be found in the UserProfileService.asmx endpoint in your O365 ADMIN site

Categories