In a program that was working, I started getting the following exception:
Activation error occurred while trying to get instance of type IRegionNavigationService
The inner exception is
InnerException {"The current type, CommonServiceLocator.IServiceLocator, is an interface and cannot be constructed. Are you missing a type mapping?"} System.Exception {System.InvalidOperationException}
Since this is part of the Prism 6 platform, I'm at a loss as to where to start to fix the problem.
The problem started when I was updating the project from a repository and the solution file became corrupted. I got the program to run, but when I choose an option that navigates to another view I get the exception.
Here is the code that gets the exception:
public class MainMenuViewModel : BindableBase, IRegionManagerAware
{
public IRegionManager RegionManager { get; set; }
public MainMenuViewModel()
{
CustomerProfileCommand = new DelegateCommand(ExecuteCustomerProfileCommand);
AdjustmentTypeCommand = new DelegateCommand(ExecuteAdjustmentTypeCommand);
StreetProfileCommand = new DelegateCommand(ExecuteStreetProfileCommand);
LocationMaintenanceCommand = new DelegateCommand(ExecuteLocationMaintenanceCommand);
}
private void ExecuteLocationMaintenanceCommand()
{
RegionManager.RequestNavigate(RegionNames.ContentRegion, NavigationNames.LocationMaintenance);
}
private void ExecuteStreetProfileCommand()
{
RegionManager.RequestNavigate(RegionNames.ContentRegion, NavigationNames.StreetMaintenance);
}
private void ExecuteAdjustmentTypeCommand()
{
RegionManager.RequestNavigate(RegionNames.ContentRegion, NavigationNames.AdjustmentTypeMaintenance);
}
private void ExecuteCustomerProfileCommand()
{
RegionManager.RequestNavigate(RegionNames.ContentRegion, NavigationNames.CustomerProfile);
}
public ICommand CustomerProfileCommand { get; set; }
public ICommand AdjustmentTypeCommand { get; set; }
public ICommand StreetProfileCommand { get; set; }
public ICommand LocationMaintenanceCommand { get; set; }
}
The RegionManager is instantiated by a region behavior and (using debug to verify) is actually instantiated.
Any direction as to where I should start is appreciated.
OK, I found the problem. I had upgraded the CommonServiceLocator package to 2.03. Apparently, Prism 6.30 only works with version 1.3.0. After I made that change, the program worked.
Related
In AttachmentViewModel I have the following code
public ICommand EditAttachmentCommand { get; private set; }
public AttachmentsViewModel()
{
EditAttachmentCommand = new ActionCommand<AvailAttachment>(EditAttachment);
}
private void EditAttachment(AvailAttachment attachment)
{
var attachmentDetailsViewModel = Router.ResolveViewModel<AttachmentDetailsViewModel>(true, ViewModelTags.ATTACHMENT_DETAILS_VIEWMODEL);
attachmentDetailsViewModel.NavigateToAttachment(attachment.ArticleId);
EventAggregator.Publish(ViewTags.ATTACHMENT_DETAILS_VIEW.AsViewNavigationArgs());
EventAggregator.Publish(new CurrentViewMessage(ContentView.Attachment));
}
In my MainViewModel I have the following code:
public ICommand SessionAttachmentCommand { get; private set; }
public MainMenuViewModel()
{
SessionAttachmentCommand = new ActionCommand<AvailAttachment>(EditAttachment);
}
private void EditAttachment(AvailAttachment attachment)
{
var attachmentDetailsViewModel = Router.ResolveViewModel<AttachmentDetailsViewModel>(true, ViewModelTags.ATTACHMENT_DETAILS_VIEWMODEL);
attachmentDetailsViewModel.NavigateToAttachment(attachment.ArticleId);
EventAggregator.Publish(ViewTags.ATTACHMENT_DETAILS_VIEW.AsViewNavigationArgs());
EventAggregator.Publish(new CurrentViewMessage(ContentView.Attachment));
}
I would like to pass the object state of AvailAttachment class from AttachmentsViewModel to MainMenuViewModel. presently null value comes for AvailAttachment's attachment object in MainMenuViewModel. I am debugging a code written by someone. This is a silverlight project using MVVM model. How do I do that?
Any help is appreciated. Thanks
I'm in a scenario WPF MVVM EF code first.
All start my application I have to initialize the two databases, but lose 10 seconds each time to do everything. there is a way to speed up?
private void InitDb()
{
var sednaComuniContext = new SednaComuniContext();
sednaComuniContext.Database.Initialize(false);//slowly
if (String.IsNullOrEmpty(Ditta) || Ditta == "BaseDb") return;
var sednaContext = new SednaContext(dbHelper.CreateConnectionString(Ditta));
sednaContext.Database.Initialize(false);//slowly
}
SednaComuniContext:
public class SednaComuniContext : DbContext,IContext
{
public static DatabaseHelper dbHelper;
static string _connection;
public SednaComuniContext()
: base(CreateConnectionString())
{
}
private static string CreateConnectionString()
{
dbHelper = new DatabaseHelper();
return dbHelper.CreateConnectionString("ArchiviComuni");
}
public SednaComuniContext(string connString)
: base(connString)
{
_connection = connString;
if (!Database.Exists())
{
Database.Initialize(true);
}
}
public DbSet<ArticoliFamiglia> ArticoliFamiglia { get; set; }
public DbSet<ArticoliGruppo> ArticoliGruppo { get; set; }
public DbSet<Articoli> Articoli { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<SednaComuniContext, Configuration>());
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
SednaContext:
public class SednaContext : DbContext,IContext //IDbContext
{
public static DatabaseHelper dbHelper;
static string _connection;
public SednaContext()
: base(_connection)
{
}
public SednaContext(string connString)
: base(connString)
{
_connection = connString;
}
public DbSet<ArticoliFamiglia> ArticoliFamiglia { get; set; }
public DbSet<ArticoliFamigliaImpostazioni> ArticoliFamigliaImpostazioni { get; set; }
public DbSet<ArticoliGruppo> ArticoliGruppo { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
Database.SetInitializer(new MigrateDatabaseToLatestVersion<SednaContext, Configuration>());
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
}
}
Can an entity migration be used for that?
Since the Entity Framework team has moved to Entity Framework 6 to the EntityFramework.dll the initial load of the database slowed down considerably, some of these problems were fixed but building a model with 2 entities at a console project takes 4-5 seconds for me as well. See this open item describing part of this problem:
http://entityframework.codeplex.com/workitem/2298
The only workaround for this would be a loading screen where you show the users some fancy progress bar and display terms like loading this and that important part of the application.
I am working on setting up a new project using Code First for entity framework 5 in silverlight using RIA services. I have created a test project due to some issues I have encountered and will post the code below.
Namely, I get an 'Object reference not set to an instance of an object' error anytime I attempt to build the silverlight client project which should generate the client proxy classes.
Just to be clear, this error is not while running or debugging the application, but when building it.
I have isolated that this only happens if I have any navigation properties/Foreign Keys defined on my Code First classes.
Any help tonight would be greatly appreciated.
public class Person
{
public int PersonId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime? BirthDate { get; set; }
public virtual List<Character> Characters { get; set; }
}
public class Character
{
public int CharacterId { get; set; }
public int PersonId { get; set; }
public virtual Person Person { get; set; }
public string CharacterName { get; set; }
}
public class CharacterDbContext : DbContext
{
public DbSet<Person> Persons { get; set; }
public DbSet<Character> Characters { get; set; }
public CharacterDbContext()
{
if (HttpContext.Current == null)
{
Database.SetInitializer<CharacterDbContext>(null);
}
}
}
[EnableClientAccess]
public class CharacterDbService : DbDomainService<CharacterDbContext>
{
#region Basic Methods for Person with the context property of Persons
[Query]
public IQueryable<Person> GetPersons()
{
return DbContext.Persons;
}
[Insert]
public void InsertPerson(Person entity)
{
DbEntityEntry<Person> entityEntry = DbContext.Entry(entity);
if (entityEntry.State != EntityState.Detached)
{
entityEntry.State = EntityState.Added;
}
else
{
DbContext.Persons.Add(entity);
}
}
[Update]
public void UpdatePerson(Person entity)
{
DbContext.Persons.AttachAsModified(entity, ChangeSet.GetOriginal(entity), DbContext);
}
[Delete]
public void DeletePerson(Person entity)
{
DbEntityEntry<Person> entityEntry = DbContext.Entry(entity);
if (entityEntry.State != EntityState.Deleted)
{
entityEntry.State = EntityState.Deleted;
}
else
{
DbContext.Persons.Attach(entity);
DbContext.Persons.Remove(entity);
}
}
#endregion
#region Basic Methods for Character with the context property of Characters
[Query]
public IQueryable<Character> GetCharacters()
{
return DbContext.Characters;
}
[Insert]
public void InsertCharacter(Character entity)
{
DbEntityEntry<Character> entityEntry = DbContext.Entry(entity);
if (entityEntry.State != EntityState.Detached)
{
entityEntry.State = EntityState.Added;
}
else
{
DbContext.Characters.Add(entity);
}
}
[Update]
public void UpdateCharacter(Character entity)
{
DbContext.Characters.AttachAsModified(entity, ChangeSet.GetOriginal(entity), DbContext);
}
[Delete]
public void DeleteCharacter(Character entity)
{
DbEntityEntry<Character> entityEntry = DbContext.Entry(entity);
if (entityEntry.State != EntityState.Deleted)
{
entityEntry.State = EntityState.Deleted;
}
else
{
DbContext.Characters.Attach(entity);
DbContext.Characters.Remove(entity);
}
}
#endregion
}
Your foreign key fields aren't mapped, thus they can't be interpreted by the proxy code generator (the piece of code called to build your proxy during compilation).
You should put in you DbContext something like
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Character>()
.HasRequired(x=> x.Person)
.WithMany(x=> x.Characters)
.HasForeignKey(x=> x.PersonId);
}
also, I suggest you to change your public virtual List<Character> Characters { get; set; }
to public virtual ICollection<Character> Characters { get; set; } , because I'm not sure if the proxy generator (and EF too) will map that list correctly.
EDIT:
I'm thinking that the EF Metadataprovider isn't supplying the correct attribute in description.
Put a KeyAttribute over the Character.CharacterId and Person.PersonID, also, add this line over Character.Person
[Association("Character_Person", "PersonId", "PersonId", IsForeignKey = true)]
and this one over Person.Characters
Association("Character_Person", "PersonId", "PersonId")]<br>
EDIT:
After chat with KitKat we finally found the problem. During proxy generation a call to Assembly.GetExportedTypes crashed complainig it need EF 4.1.
Simple putting
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="EntityFramework" publicKeyToken="b77a5c561934e089" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-5.0.0.0" newVersion="5.0.0.0" />
</dependentAssembly>
</assemblyBinding>
in the related config did the tricks
Note: at this link ther's blog post from mine that better explains how to deal with EF5 Code first and WCF Ria Services
I have a WCF operation MyGetVersion() that returns a System.Version. When debugging a call to it from a Silverlight service reference, I verified that the service returns the correct System.Version object. In the service reference, the auto-generated method:
public System.Version EndMyGetVersion(System.IAsyncResult result) {
object[] _args = new object[0];
System.Version _result = ((System.Version)(base.EndInvoke("MyGetVersion", _args, result)));
return _result;
}
raises the exception:
Attempt by method 'DynamicClass.ReadVersionFromXml(System.Runtime.Serialization.XmlReaderDelegator, System.Runtime.Serialization.XmlObjectSerializerReadContext, System.Xml.XmlDictionaryString[], System.Xml.XmlDictionaryString[])' to access method 'System.Version..ctor()' failed.
I had to turn on the "break on CLR exception" helper to see this. Otherwise, it is a TargetInvocationException. The System.Version() constructor is public as far as I can tell. What am I doing wrong?
The problem is that the constructor of System.Version is public in the .NET Framework, but it's not in Silverlight (it's internal, according to Reflector). So while the type is serializable in the full framework, it's not in Silverlight, and the Add Service Reference tool should have replaced it with an equivalent type in SL - this is a bug in the tool (I'll report it to the product team, thanks for finding it).
As workarounds, I'd suggest to use a "surrogate" type for Version, and use it in your service contract for data transfer only:
[DataContract]
public class VersionDTO
{
[DataMember]
public int Major { get; set; }
[DataMember]
public int Minor { get; set; }
[DataMember]
public int Build { get; set; }
[DataMember]
public int Revision { get; set; }
public VersionDTO(Version version) {
this.Major = version.Major;
this.Minor = version.Minor;
this.Build = version.Build;
this.Revision = version.Revision;
}
}
[ServiceContract]
public interface ITest
{
[OperationContract]
VersionDTO GetVersion();
}
Another option, given the issue you mentioned in the comment, would be to replace the reference to the Version class in the generated proxy for Silverlight with a class which is equivalent to it. The class below can be used to deserialize a Version object in SL from .NET.
[DataContract(Name = "Version", Namespace = "http://schemas.datacontract.org/2004/07/System")]
public class SLVersion
{
[DataMember(Order = 1, Name = "_Build")]
public int Build { get; set; }
[DataMember(Order = 2, Name = "_Major")]
public int Major { get; set; }
[DataMember(Order = 3, Name = "_Minor")]
public int Minor { get; set; }
[DataMember(Order = 4, Name = "_Revision")]
public int Revision { get; set; }
}
I am having problems databinding to EF code first. I need to be using Eager Loading, but I am running into some issues with databinding. I have the following classes:
public class Context : DbContext
{
DbSet<A> As;
DbSet<B> Bs;
DbSet<C> Cs;
}
public class A
{
public ICollection<B> Bs { get; set; }
public string Name { get; set; }
}
public class B
{
public ICollection<C> Cs { get; set; }
public string Name { get; set; }
}
public class C
{
public string Name { get; set; }
}
I am data binding Context.As to a Treeview, using the below code:
Context.As.Load();
tvItems.ItemsSource = Context.As.Local;
This works as expected, however, it does not automatically load the child properties, Bs, and subsequently, Cs. So, I found that lazy loading can help with this, like so:
Context.As.Load();
tvItems.ItemsSource = Context.As.Include(u=>u.Bs);
From my reading, this should automatically load at least the first level of child properties. However, this will not data bind, as I did not use .Local
.Include() returns IQueryable, which does not support .Local. I can use .ToList(), but this will not automatically update when I add items.
So, how the hell am I supposed to be doing this?
You could try this:
Context.As.Include(a => a.Bs).Load();
tvItems.ItemsSource = Context.As.Local;