Customize table names ASP MVC 4 - asp.net-mvc-4

I have a few tables previously created in my database that I want to map to a few model classes:
"SISTEMA.Voyages" => public class Voyage
"SISTEMA.Puerto" => public class Port
I understand that in ASP.MVC 4 with Entity framework this can be done either of two ways. However for both of them I am getting errors which I do not know how to resolve.
The first in Voyage.cs:
[Table("SISTEMA.Voyages")]
public class Voyage
Or the second in Context.cs:
public class ApplicationEntities : DbContext
{
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Voyage>().ToTable("SISTEMA.Voyages");
}
}
For the first version I get this error when I previously assumed this was something automatic:
The type or namespace name 'Table' could not be found (are you using directive or an assembly reference?)
The type or namespace name 'TableAttribute' could not be found (are you using directive or an assembly reference?)
Fore the second I get this error which I didn't expect because I assumed this was a configuration issue and leaves me really confused:
The model backing the 'ApplicationEntities' context has changed since the database was created. Consider using Code First Migrations to update the Database.
Where is this history even recorded?
For the record, I am used to dealing with this sort of issue in Rails by typing in:
class Voyage
self.table_name = "SISTEMA.Voyages"
end
And I am not very familiar with C# / ASP.NET. Just publishing what I am looking up for the next hour unless somebody tells me where I am going wrong first.

For the first way, you are missing a using directive for the Table attribute, add this to your class:
using System.ComponentModel.DataAnnotations;
For the second way, I'm guessing that something has changed in your model and now it recommends using Code First to update the database. When you run your application, and it first hits the database, it verifies that your models match the schema - if not it can give you this error.
I recommend checking out some of these links for help getting started with EF / Code First / Migrations - they are really handy
EF + Code First
Handling Many to Many with Payload
How to work with Migrations
MSDN EF Site

Please Add:
using System.ComponentModel.DataAnnotations.Schema;

Related

AspNet Core WebApi OData - Many to Many join tables

As is the recommended pattern in EF Core these days when you have a many to many join we do something like this:
Fluent API, many-to-many in Entity Framework Core
Having done that I'm faced with the issue as to how I go about exposing that in the OData model.
Since technically the Entity type definition has no key property (as it uses a composite key the OData framework doesn't like me adding the set to the model.
Whats the recommended approach to this problem?
It seems that EF and OData have become somewhat synced up, what would be even better would be if they could literally share model building code.
To that end I found that in the OData model build after calling AddSet I was able to define the key in the same way as I did in EF ...
Builder.EntityType<UserRole>().HasKey(ac => new { ac.UserId, ac.RoleId });
This is somewhat clean, I have not yet tried posting or directly requesting such a type yet, but expanding from either side of the relationship chain seems to work fine.
EDIT: Moredetails on the definition of this in controller ...
The Url needs to contain both key parts ...
HTTP GET ~/UserRole(123, 456)
the same with PUTs and DELETEs but POST's don't contain the key they are part of the object in the body.
The method signature must contain both key parts, here's some examples ...
public IActionResult Get([FromODataUri]int keyUserId, [FromODataUri]Guid keyRoleId)
{
...
}
public IActionResult Put([FromODataUri]int keyUserId, [FromODataUri]Guid keyRoleId)
{
...
}
public IActionResult Post(UserRole entity)
{
...
}

RavenDB does not set all the properties on store

I have a really weird scenario where I try to store domain events (I'm trying to learn CQRS and RavenDB at the same time). The basic structure of the documents I try to store are:
public interface IDomainEvent { ... }
public abstract class BaseDomainEvent : IDomainEvent { ... }
public class DomainEventA : BaseDomainEvent { ... }
public class DomainEventB : BaseDomainEvent { ... }
Given that I want to store DomainEventA and DomainEventB in the same collection in RavenDB and I have managed to do so. But the problem is that in the collection I am missing the properties of DomainEventB, and not all properties are set even though I have checked that the properties are set before I commit the transaction where I store the objects. The following gist shows a working example of what I want to do: https://gist.github.com/2830093, and the test code that fails me is found in this test: https://github.com/mastoj/TJ.CQRS/blob/ravenfail/TJ.CQRS.RavenEvent.Tests/RavenEventStoreTests.cs that is using this RavenDB code: https://github.com/mastoj/TJ.CQRS/blob/ravenfail/TJ.CQRS.RavenEvent/RavenEventStore.cs.
I really can't get my head around this one.
EDIT 1: I can add that in the failing scenario the metadata of the stored object says it is one type but the properties for that type is not stored.
I planned to delete or vote for close but I think more than me might experience this problem at some point. I found the solution in my case and it was that the objects I added to RavenDB had a faulty equals method so RavenDB thought that all my objects were the same one. When I added one more property to check in the equals method everything start working as expected.

Auto generated linq class is empty?

This is a continuation of my previous question: Could not find an implementation of the query pattern
I'm trying to insert a new 'Inschrijving' into my database. I try this with the following code:
[OperationContract]
public void insertInschrijving(Inschrijvingen inschrijving)
{
var context = new DataClassesDataContext();
context.Inschrijvingens.InsertOnSubmit(inschrijving);
dc.SubmitChanges();
}
But the context.Inschrijvingens.InsertOnSubmit(inschrijving); gives me the following error:
cannot convert from 'OndernemersAward.Web.Service.Inschrijvingen' to 'OndernemersAward.Web.Inschrijvingen'
I call the method in my main page:
Inschrijvingen1Client client = new Inschrijvingen1Client();
Inschrijvingen i = new Inschrijvingen();
client.insertInschrijvingCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(client_insertInschrijvingCompleted);
client.insertInschrijvingAsync(i);
But as you can see there appears to be something wrong with my Inschrijvingen class, which is auto generated by LINQ. (Auto generated class can be found here: http://pastebin.com/QKuAAKgV)
I'm not entirely sure what is causing this, but I assume it has something to do with the auto generated class not being correct?
Thank you for your time,
Thomas
The problem is that you've got two Inschrijvingen classes - one in the OndernemersAward.Web.Service namespace, and one in the OndernemersAward.Web namespace.
You either need to change the codebase so that you've only got one class, or you need to convert from one type to the other.

objectcontext vs datacontext

i'm just new in developping silverlight, and i created a linq to sql connection and a service domain class. I want to get data from 2 tables which have a 1 to many relation into a datagridview. To do this i need to state include commands in my metadata and service domain class , but to do this i need to have an objectcontext instead of a datacontext(that i'm currently having ) can someone help me with this matter so i can use the include statement to get querys for my detail-grid
edit:
I've done what u said added the
"<IncludeAttribute()> _"
Public Property SubgroepIndustries As EntitySet(Of SubgroepIndustrie)
but i get this error message:
Error 1 'Include' is not a member of 'System.Data.Linq.Table(Of ILA4.HoofdgroepIndustrie')
edit 2:
when i try to use the include in my domain service class not the metadata so
Return Me.DataContext.HoofdgroepIndustries.Include("SubgroepIndustries")
doesnt work
ObjectContext is a class that is generated inside the generated DomainService class that you made.
Just do a this.ObjectContext in the DomainService class you made and you should have access to the class you are looking for.
I have assumed here that you are using RIA services and your DomainService MetaData class is tagged with [Include] attributes. Otherwise doing this.ObjectContext.SomeEntity.Include("ChildEntity") will not work out.
Edit:
Add <IncludeAttribute()> _ to Public Property SubgroepIndustries As EntitySet(Of SubgroepIndustrie) in your .metadata.vb
As for ObjectContext, looking at your code you don't need ObjectContext I think. Use DataContext instead.
so for example:
Public Function GetHoofdgroepIndustries() As IQueryable(Of HoofdgroepIndustrie)
Return Me.DataContext.HoofdgroepIndustries.Include("SubgroepIndustries")
End Function
is how you will do it.
Edit 2: You need Imports System.ServiceModel.DomainServices.Server for <IncludeAttribute()>

Problem with RavenDB 'Hello World' tutorial

I am going through the RavenDB tutorial on the RavenDb.net website.
It was going fine until I got to the code block for creating an index.
This code segment is direct from RavenDB.Net website.
store.DatabaseCommands.PutIndex("OrdersContainingProduct", new IndexDefinition<Order>
{
Map = orders => from order in orders
from line in order.OrderLines
select new { line.ProductId }
});
I get an error on compile: "The non-generic type 'Raven.Database.Indexing.IndexDefinition' cannot be used with type arguments."
If IndexDefinition is non-generic, why is it used as generic in the sample code? Where is the disconnect?
Thank you for your time
Jim
Depending on your using statements you may be referencing the wrong IndexDefinition class (from another Raven assembly). Try adding this to the beginning of your file:
using Raven.Client.Indexes;
You might need to remove other using statements as well. I guess this is one reason why Microsoft recommends using unique names for classes even in the presence of namespaces.