c# - Query a table using Code First and WinForms -


i have 1 user in table users. simple want query records in table. want populate list variable.

database: testtf

table: users userid int identityfield username nvarchar(64)

app.config

<configuration>     <configsections>     <!-- more information on entity framework configuration, visit http://go.microsoft.com/fwlink/?linkid=237468 -->         <section name="entityframework" type="system.data.entity.internal.configfile.entityframeworksection, entityframework, version=5.0.0.0, culture=neutral, publickeytoken=b77a5c561934e089" requirepermission="false" />   </configsections>   <startup>     <supportedruntime version="v4.0" sku=".netframework,version=v4.5" />   </startup>   <connectionstrings>         <add name="testef" connectionstring="data source=.;initial catalog=testef;integrated security=true" />   </connectionstrings>   <entityframework>         <defaultconnectionfactory type="system.data.entity.infrastructure.localdbconnectionfactory, entityframework">       <parameters>         <parameter value="v11.0" />       </parameters>     </defaultconnectionfactory>   </entityframework> </configuration> 

testefcontext.cs ( context being used )

public class testefcontext : dbcontext {     public dbset<user> users { get; set; }      protected override void onmodelcreating(dbmodelbuilder modelbuilder)     {          base.onmodelcreating(modelbuilder);          modelbuilder.configurations.add(new userconfig());     } } 

userconfig.cs

public class userconfig : entitytypeconfiguration<user> {     public userconfig()     {         haskey(x => x.userid);     } } 

user.cs

public class user {     public int userid { get; set; }     public string username { get; set; } } 

code in winform:

public partial class form1 : form {     public form1()     {         initializecomponent();         testefcontext ctx = new testefcontext();          list<user> list = (from d in ctx.users                     select d).take(5).tolist();          foreach (user user in list)             debug.writeline(user.username);          this.combobox1.displaymember = "username";         this.combobox1.datasource = list;     } 


Comments

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -