How to get lightswitch table data in mvc controller? - c#

I am new and beginner in light-Switch,
I am doing light-Switch with MVC.
Let me explain my scenario:
First I have created one light switch application. In this app having one project is called [ProjectName].Server
Then I've Connect my SQL database in Light-Switch application. And try CRUD operation via Light-Switch screen and its working fine.
In This server project I've create MVC structure like controller, model and views. using Click Here
Then I've create simple Index view in Home controller and its open successfully.(via open browser from desktop client)
Now i want to get light-Switch table data(records) in my controller via entity Framework or light-Switch data context. suggest me easiest way..
I don't know how to get data in controller using light-switch data context. so I've try with entity framework using DbContext and DbSet.
But i got error:
One or more validation errors were detected during model generation:
LightSwitchApplication.Data.[EntityName]: EntityType '[EntityName]' has no key defined. Define the key for this EntityType.
[EntityName]: EntityType: EntitySet '[EntityName]' is based on type '[EntityName]' that has no keys defined.
I've also got some solution for light-switch data context here Click Here
I've try to implement but not getting ApplicationData and CreateContext See Attached.
Your answer will appreciable
Thanks,
Jatin

See my article: An HTML MVC LightSwitch Security Administration (http://lightswitchhelpwebsite.com/Blog/tabid/61/EntryId/3281/An-HTML-MVC-LightSwitch-Security-Administration.aspx) for an example.
Basically you need to have these includes:
using Microsoft.LightSwitch.Server;
using Microsoft.LightSwitch;
using LightSwitchApplication.Models;

Related

Choosing DB context when adding identity to existing project

I'm trying to add Identity to an existing and working MVC project.
When used this command Add-Migration CreateIdentitySchema I got error message: 'More than one DbContext was found. Specify which one to use. Use the '-Context' parameter for PowerShell commands and the '--context' parameter for dotnet commands.' and I chose the existing DB context that I have previously created for the main functionality of the application and now it shows 'There is already an object named 'ExpenseReport' in the database.' so it's trying to recreate my initial model.
Should I have a separate DB Context for Identity and if yes how to connect user to data from the other context?
So, i think you can use several DbContexts with different connections (just need to write it in your Startup class) or you need to use pattern "DbContext Factory". You can read more here .
Hope it will help you.

Entity Framework code-first filter data on database and read it from EF

I have a C# project with EF code-first approach. The purpose is to do crud operations. My web app will be hosted under IIS under two folders
web app
WCF service
The application will pass user credentials to the WCF service and then to SQL Server database. Code first approach works well in this case.
READ scenario: the data needs to be filtered on the database level, not on application level. So I cannot directly call tables while querying SQL using EF. User will not have read access on the tables but only on views. The SQL views will have responsibility to filter the data and then pass to the service.
The question is how to proceed with EF code first approach in a way that I can leverage the benefits of EF migration s and then map the tables to the SQL views. Can anyone explain how to proceed with this read scenarios and best approach?
Entity Framework doesn't support mapping to Sql views but you can do the following:
Create an entity class that mapping your (sql) view structure.
Add a new empty migration using the add-migration -IgnoreChanges command since you don't want your entities to map to tables.
In this migration you can create (Up method) your Sql view.
It is important to note that you have to indicate the Sql view name in your entity ("view entity?") configuration (mapping) class:
public class FooConfiguration : EntityTypeConfiguration<Foo>
{
public FooConfiguration()
{
ToTable("ViewName");
HasKey(p => p.Id); // You also have to configure a key
}
}
This article from Julie Lerman could be interesting for you: https://msdn.microsoft.com/en-us/magazine/dn519921.aspx

Generate database from data model

I work with .net4. I've created an ADO.NET Entity Framework data model and add new entity with scolre property.
I've created a Database1.mdf with a data set.
Now I click on Generate database from model -->
select `Database1ConnectionString` -->
create `Model1.edmx.sql`
Now when I click on Execute sql -->
servername=`username\SQLEXPRESS` -->
Click on connect
I get an error:
Database 'Database1' does not exist. Make sure that the name is
entered correctly.
Why?
Select a corresponding template in the model properties window. Then right-click on the diagram and run the Create Database from Model wizard by choosing Generate Database from Model from the popup menu.
Note:
The default schema name for Microsoft SQL Server database is dbo. Don't forget to change it to the necessary one before the code generation.
Here is a DDL script generated for the current model.
As a result of these steps you will obtain a valid database-specific DDL script. You can run this script against your database to create a valid database schema.

ASP.NET MVC3 linking posts to the account models using code first

I have a newly created ASP.net MVC3 project with all the defaults. I am using the approach to build the database. I come from a PHP background and this is my first attempt at a ASP.net project.
I created a new model called "posts" and I want to link it to the built in accounts/users system that came with the default project.
Only signed in users can add posts, and I want to know what user added what post. I want to link the posts to a single user.
In PHP I would create a FK in the posts table to the users table "posts_id" and when a user adds a new post I would fill in that field with the users's id. But there does not seem to be a ID field in the account model.
Questions
How do I link a model to the default account system build in to ASP.net MVC3 using code first approach?
How do I query for user fields from a view in the post controller?
Links to a tutorials would work for me.
Thanks
FKs like posts_id is a database style reference. The object oriented way of representing that data is by giving the Account object a ICollection<Post> and by giving each Post object an Account reference field.
E.g.
public class Post
{
public Account Account { get; set; }
...
}
As for retrieving this data, I would suggest a repository-style service class which would be responsible for saving/loading objects to & from the DB. I am not sure exactly how the Account records get persisted in the template MVC app so I can't say how it would tie into that data. I have always used my own User DB table and thrown away all that build in Microsoft account stuff.
Good luck!

Linq query combining ADO.Net entities from a database connection and a data service reference?

I'm not even quite sure how to properly word this, but here goes.
I have two .net web applications. One provides a WCF data service ("DEPT_DataService") which provides access to a number of entity sets ("DEPT_Entities"). The other is an MVC2 application which provides a web interface, and also has its own ADO.net entities which are backed by a local MSSQL database.
Using an overly-basic structure to illustrate:
Let's say the WCF data service application includes a single entity set, "Departments", backed by a "Departments" table in its local SQL database. This contains a list of all the departments in the company, along with the employee ID of the primary contact for that department.
Let's also say that the MVC2 application includes a single entity set, "Employees", which contains a list of a bunch of people, including their name and their employee ID.
I've got DEPT_Entities added to the MVC2 application as a Service Reference, "DEPT". When I look at that service reference in the Object Browser, I see "DEPT_Entities" and "Departments".
What I want to do is define a relationship that allows me, via Linq, to refer to something like this:
Employee firstEmployee = db.Employees.First();
Department[] firstEmployeesDepts = firstEmployee.Departments.toList();
...in other words, I essentially want a navigation property that provides a one-to-many relationship between the Employee entities found in the local database and the Department entities found in the remote data service.
Is this doable? How?
Thanks!
If you want to access the databases directly without a webservice:
You can use SQL Server tricks (Remote Query inside of a View to a Linked Server) to make Linq2SQL be able to query the remote table. Essentially, you simulate that the remote table is just an ordinary local table (a view, but L2S does not care).
This will work but it will not be pretty.
If you want to have a webservice in front of one of the tables: This will not work because L2S does not understand webservices.
In fact, I would architecturally advise against hinding the fact that you are calling a webservice.

Categories

Resources