Browse Database EF ASP.Net Core MVC - entity-framework

I'm playing around with a web application using the above and visual studio 2016 community. I've pretty much still got the default project using Individual User Account as the authentication. So it's built all the views and models around authentication and I can register accounts and log in. I'm now wanting to add some attributes to the default and running into some unknowns.
First question is about writing some non-default attributes. I've looked into how it's writing the default attributes (email and phone) and there are actually methods in the UserManager library class to update these attributes
public virtual Task<IdentityResult> SetEmailAsync(TUser user, string email);
and
public virtual Task<IdentityResult> SetPhoneNumberAsync(TUser user, string phoneNumber);
Obviously I can't add methods for my own attributes like SetTagLineAsync... because this is a library class. I found it a little strange that they have such specific methods in a library but anyway I have the user object in context in my controller (which you can see above in the virtual methods) and I think I should be able to just update that object and call this method
public virtual Task<IdentityResult> UpdateAsync(TUser user);
And it'll update the user in the database to use the attributes I've set on the user. But this leads into my second question, because I'm not certain how this works yet I would want to try the above method and then go check in the database if it did update the attribute I wanted.
Second question: How do I view the projects database? There is obviously a database that was created with the project template and it is persistent because if I run the web app and register, when I run it again I can log in as that user. When I make a change to the ApplicationUser class I also have to add a migration and update the database through package manager console. I can see in my app settings
"DefaultConnection": "Server=(localdb)\\mssqllocaldb;Database=aspnet-MyApp-3B07911E-A6DD-4B3E-A533-57B04E37B791;Trusted_Connection=True;MultipleActiveResultSets=true"
I don't have SQL server installed if that matters, just whatever sql stuff was installed with VS. In other projects i just go to Server Explorer and my database appears under Data Connections. I've tried to add the data connection but when I do, I add the server name (localdb) – i also tried without the brackets – and then when I try to select a database name it throws connection errors. I've also tried doing this while the webapp is open by disconnecting the debugger and trying the same process in case it only exists while it's running but same thing. Any ideas?

Obviously I can't add methods for my own attributes like SetTagLineAsync...
You can add your own method(s) inheriting from UserManager class.
UpdateAsync vs. SetXXXAsync methods
SetXXXAsync methods are more complex and have additional logic inside. For example SetEmailAsync and SetPhoneAsync are not only setting Email and Phone property/column, but also EmailConfirmed = false and PhoneConfirmed = false. Both are also setting new security stamp that is used to indicate, if user changed profile. In that case he/she needs to perform new login on (other) devices(rejecting cookies that was set before the change happened). Which is useful security feature. It will be really bad if you change your password, but cookies set with old one will still work.
public virtual async Task<IdentityResult> SetPhoneNumberAsync(TUser user, string phoneNumber)
{
ThrowIfDisposed();
var store = GetPhoneNumberStore();
if (user == null)
{
throw new ArgumentNullException(nameof(user));
}
await store.SetPhoneNumberAsync(user, phoneNumber, CancellationToken);
await store.SetPhoneNumberConfirmedAsync(user, false, CancellationToken);
await UpdateSecurityStampInternal(user);
return await UpdateUserAsync(user);
}
How do I view the projects database?
You have to use (localdb)\MSSQLLocalDB as server name.
LocalDb is development database and may be installed during Visual Studio installation.
Related links:
Introduction to Identity on ASP.NET Core
UserManager class (source code)
What is ASP.NET Identity's IUserSecurityStampStore interface?
How to connect to LocalDB in Visual Studio Server Explorer?
Introducing LocalDB, an improved SQL Express

Related

IdentityServer3 Entity Framework migrate from v2 MembershipProvider

We use IdentityServer v2 in a production environment for over 2 years now. We'd like to move to v3 ou even 4, but I don't really see how we could migrate users account which are actually in a MembershipProvider DB to the new schema defined in the project IdentityServer3.EntityFramework. The problem is that I have no clue on how I could accomplish that. Anyone has ever done this ?
I don't see a scenario where I can add Scopes or Clients either. I guess that I have to create Scope object in code and save it to context, but since I'm not a pro in EF, I'm not really sure about the best practices. Can anyone point me to a sample ? The one in GitHub project doesn't seem to add Scopes or Clients, it just manage the schema with Migration (unless I'm mistaken).
EDIT : I got the sample working so I figured out the adding part in the Factory class. The Migration configuration part is explained here. There's a comment block in the ClientConfiguration Seed method where it shows how to use the AddOrUpdate extension. The Configuration class extends DbMigrationsConfiguration so you use it when you run the Update-Database command as the ConfigurationTypeName parameter.
So only question left is : how to update from MembershipProvider ?

How to disable code-first feature in EF (MVC4 Visual Studio 2012)

How to disable code-first feature in EF (Visual Studio 2012)
I am using Visual Studio 2012, MVC4 (Internet application template).
I want to use EF, but not with its code-first feature. I would want the application to error out, rather than create or modify my database based on my code. (i just can not live with this feeling of my database being changed behind the scenes ... i want the application to use the exact db i have created ... and if there is any thing that has to be changed, i'll do it my self)
is this possible with the new Ef (VS2012)?
i have seen many people asking this, but so far i am unable to find the answer.
You can use Code First and ensure that your database never gets updated or overwritten when you change your model by setting the database initializer to null:
Database.SetInitializer<MyDbContext>(null);
It's a static method of the Database class and should be called at the beginning of your application, for example in global.asax or in a static constructor of your context class. Doing this you have to change model class and database schema manually so that they match.
You can also use the Reverse Engineer feature to create a Code First model from an existing database. It is explained here: http://msdn.microsoft.com/en-us/data/jj200620
Or if you don't want to use Code First at all and work with a model designer you can use the Database First approach, explained here: http://msdn.microsoft.com/en-us/data/jj206878
An overview about all the possible options is here: http://msdn.microsoft.com/en-us/data/ee712907.aspx

Java EE 6 form-authentication and FacesContext.isUserInRole

I'm using a form-based authentication (with a JDBC-realm) to authenticate users in my EE application. I created a /home/* section, which is only accessible if a user is in the role USER or ADMIN.
Everything is working fine, but the problem is that when I use the FacesContext.getCurrentInstance().getExternalContext().isUserInRole(role)-Method (for example to decide whether some UI-components are shown on the gui or not), the return value always is false.
I have absolutely no idea why the method always returns false. What am I missing?
The most confusing thing is, that I have implemented the exact same behaviour in another project (on the same glassfish-server-instance) and it works there.
I just found the problem: I forgot to add the #DeclareRoles() annotation.
You have to Declare the roles you want to use somewhere in your application. I for example use a SingletonEJB in which I declare the roles I'm using (using the DeclareRoles-Annotation).
Example:
#Singleton
#LocalBean
#DeclareRoles({ "ADMIN", "USER" })
public class Application {
}

EF Code First not working on deploy hosting

I've an application using MVC and Code First for persistence.
Everything works fine in my development, but when I'm uploading to server, it doesnt work.
In any place i try to create a database, but it keeps me returning the following error: CREATE DATABASE permission denied in database 'master'.
The only thing that i do is override the OnModelCreating method just to map my app.
Anyone has this error?
Thanks
For a tutorial series that shows how to publish your Code First database and prevent Code First from trying to re-create the database in production, see
http://www.asp.net/mvc/tutorials/deployment-to-a-hosting-provider
The third tutorial in the series shows how to set up the Web.config file. The second shows how to deploy if you are using SQL Server Compact, the tenth shows how to deploy to full SQL Server.
You'll need to publish your database out to your hosting provider. Visual Studio has an easy way of doing this. In the server explorer tab, you can navigate to your database, right click and choose publish to provider. By doing this, you will not only export the scheme of your database, but you can also export out all data, stored procs, views etc.
You will need to adjust your code so that you are no longer trying to create a database on code run. Typically this approach is used for development, and you are no longer in development if you're moving to a hosting company. The changes may be in your global.asax, the dbcontext of your solution and any other place where you modified it to create the scheme for the database.
Hope this helps you some, and good luck on your project.

Where does Context.Track<T> information go?

I have tried
public sealed class WriteMessage : CodeActivity
{
protected override void Execute(CodeActivityContext context)
{
context.Track(new BuildInformationRecord<Foo>()
{
Value = FooInstance,
});
}
}
and
then found an overload on context.Track that accepts <T>
context.Track(FooInstance);
and neither one appears in the build viewer in visual studio.
context.TrackBuildMessage(string data) certainly does.
and I'm not getting any exception, is it being saved to the tfs data store, or a file? or is just silently failing entirely?
Reference to one of the resources implicating how this works or should work from the code side
it actually does make it its way to the datastore. Your instance is converted into a Dictionnary<String,String> by reflection on its properties (you may provide a type converter to Dictionnary<String,String> if you like) then stored as a build record into the database. Records are logically structured as a tree. You may retrieve this info through the TFS API.
You are using a new type for TFS which is "Foo". Thus, your info cannot yet be displayed on the build reports in Visual Studio because VS doesn't know how to represent it. You actually need to extend VS build reports with a custom addin that can convert this into WPF controls for rendering.
If you track a "BuildMessage", this is a well known type by VS, it will be displayed in the build log.
You may as well develop a Warehouse adapter to bring this data from the stored build reports to the TFS warehouse store (and also the TFS cube).
About adding custom info into the build :
http://blogs.msdn.com/b/patcarna/archive/2009/11/19/tfs-2010-customizing-build-information-part-2.aspx (and have a look at part 1)
My article on my blog in french (answers exactly your question, sorry, yeah... french) :
http://blogs.codes-sources.com/vlabz/archive/2011/09/24/tfs-2010-enrichir-les-donn-es-d-un-build.aspx
Hope this helps.
(I can't post more links at the moment since I'm a new user, contact me directly I'll send you nice URLs, or google for "Changing The Way Build Information IS Displayed")