ArgumentException when creating instance of object that inherits from ObjectContext - entity-framework

I'm loosely following an excellent series of blog posts by Kazi Manzur Rashid as a learning exercise for learning how to implement some new (for me at least) design patterns, but I'm getting trouble from the start.
I've basically copied his code for the Database, RepositoryBase and RepositoryBaseTests classes, but when I try to run the tests, I get an error message saying
Unable to create instance of class Booking.Infrastructure.EntityFramework.Repositories.Tests.RepositoryBaseTests. Error: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.ArgumentException: Format of the initialization string does not conform to specification starting at index 0..
Through the debugger I have verified that the exception is thrown on the constructor for the Database class, which looks like this:
public Database(
IConfigurationManager configurationManager,
string connectionstringName)
: base(
GetConnectionString(configurationManager, connectionstringName),
"BookingEntities")
{ // Nothing happens here }
The error is thrown when calling the base constructor, and if I'd hard-code the values that I'm currently sending in, it would look like this:
: base("Dummy connStr", "BookingEntities")
Why doesn't this work?

"Dummy connStr" is not a valid EF connection string.
A valid EF connection string looks like:
connectionString="metadata=res://*/Data.Model.csdl|res://*/Data.Model.ssdl|res://*/Data.Model.msl;provider=System.Data.SqlClient;provider connection string="Data Source=SERVERNAME\SQLDEV2008;Initial Catalog=DBName;Integrated Security=True;MultipleActiveResultSets=True""

Related

Nested Transactions In EF6

I have a method where the DBContext is pass in (this is only POC code).
I then try to open another nested transaction, With this code
using (var cxt = new TestEntities(context.Database.Connection, false))
{
using (DbContextTransaction dbContextTransaction = cxt.Database.BeginTransaction())
{
The call to "BeginTransaction" gives this error:
An exception of type 'System.Data.Entity.Infrastructure.UnintentionalCodeFirstException' occurred in TestEF6.exe but was not handled in user code
Additional information: The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development. This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715
I have a Database First Model
Any clues?
Regards
GregJF

Argument Exception

Exception:
The specified named connection is either not found in the configuration, not intended to be used with the EntityClient provider, or not valid.
Method where exception happened:
/// <summary>
/// Initializes a new DSAplcEntities object using the connection string found in the 'DSAplcEntities' section of the application configuration file.
/// </summary>
public DSAplcEntities() : base("name=DSAplcEntities", "DSAplcEntities")
{
this.ContextOptions.LazyLoadingEnabled = true;
OnContextCreated();
}
What exactly does this exception mean and how can I fix it?
Found the solution. The problem was that I was using WCF Services in my project and I did not copy the connection string to the App.Config inside the service solution.
The string arguments passed to the base class constructor are used to pass in either a named connection string (defined externally in a config file) or the connection string itself, depending on which constructor is invoked.
It looks like you're invoking the constructor on ObjectContext that takes two arguments, the first of which must be a valid connection string. The string you are passing in is not a valid EF connection string, which is why you get the error message from EF.
Check if you have the actual connection string defined in your config file. Note what name it has been defined with and pass in that name as the first argument (and if that doesn't work, try removing the second argument - I'm not sure if the method you're calling accepts a named connection string).

Persisting Classes implementing an interface with GORM MongoDB Plugin

I am having a problem while persisting a class. I have a class called Scraper which uses an interface called Paginator. There are several implementations of the Paginator interface which will be instantiated at runtime. So the structure looks like this:
class Scraper {
//some code
Paginator paginator
//more code
def Scraper(Paginator paginator){
this.paginator = paginator
}
}
and then there are the concrete implementations of the paginator interface lets say paginatorA and paginatorB. So now I am trying to do the following:
PaginatorA p = new PaginatorA()
Scraper s = new Scaper(p)
s.save(flush:true)
...and what it get is:
Error Error executing script TestApp:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoDatastore': Cannot resolve reference to bean 'mongoMappingContext' while setting bean property 'mappingContext';
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoMappingContext': FactoryBean threw exception on object creation; nested exception is java.lang.NullPointerException (Use --stacktrace to see the full trace)
Can anybody tell me what to make of this? I guess it has something to do with the Mapper because it doesn't know which concrete Paginator to use or how to persist it? If that is the case then how can I tell the framework what to do? I tried to come up with a solution for hours now and am really frustrated so any help would be really really appreciated.
Oh btw I also tried implementing against the concrete implementation (PaginatorA) ... this works perfectly fine since my assumption that it has something to do with the paginator interface.
Thanks for any response...
The error is bad, you should probably raise a JIRA issue for that, but fundamentally there are 2 problems I can see with the code:
Your persistent classes must have a public no-args constructor as per any JavaBean, by adding a constructor that takes your interface, you are no longer providing one
Your Scraper class needs to mark the 'Paginator' as transient to tell the persistence engine not to attempt to persist the 'paginator' property. Since this a custom interface it will not know how to persist it.

Introspecting maximum column length using IronPython and EDM

I am engaged in an ETL project using IronPython and loading into a SQL Server database represented by an EDM 4.1 model provided to me. While I can generally load and save the data without trouble, I'm plagued by string length overruns in the incoming data. This throws an exception in the final .SaveChanges() call to the EDM. I receive no information in the exception about what entity, entity class, or property threw the error.
I would like to modify my code so that it checks the max length of the column for the database's metadata at the time of assignment. It appears that this metadata is not available through the entity class but rather using the MetadataWorkspace of the EDM context object.
In order to call .GetItems() on the MetadataWorkspace, I need to provide an enumeration value from System.Data.Metadata.Edm.Dataspace:
.GetItems(System.Data.Metadata.Edm.Dataspace.CSpace)
Unfortunately, I don't seem to be able to reference System.Data.Metadata in any way. I cannot get beyond System.Data:
>>> import clr
>>> clr.AddReference("System.Data")
>>> import System.Data
>>> clr.AddReference("System.Data.Metadata")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: System.IO.IOException: Could not add reference to assembly System.Data.Metadata
at IronPython.Runtime.ClrModule.AddReference(CodeContext context, String name)
Does anyone have experience in introspecting EDM metadata from IronPython?
To be able to access MetadataWorkspace you need to add reference to System.Data.Entity.dll (if you don't have one) and then drop down to ObjectContext like this:
((IObjectContextAdapter)ctx).ObjectContext.MetadataWorkspace
where ctx is your DbContext derived class.
Also in EF 4.1 there is a Validation feature implemented. I am not sure if this what throws the exception because it should give you all the details - including property name and a reference to the entity where the validation error occured. If the exception is thrown by the database it would mean that Validation is turned off. If you turned it on it would check facets and throw an execption if they are validated. Here are the links to blog posts on Validation:
http://blogs.msdn.com/b/adonet/archive/2011/05/27/ef-4-1-validation.aspx
http://blogs.msdn.com/b/adonet/archive/2010/12/15/ef-feature-ctp5-validation.aspx

GWT RequestContext ENum in the request

If we use enum as one of the attribute in the Request invocation , it throws an UnsupportedOpeationException and does not even invoke the service method on the server.
#Service(value = DesignService.class, locator = DesignServiceLocator.class)
public interface DesignRequest extends RequestContext {
Request<List<DesignProxy>> findDesign(SortEnum sortorder);
}
when we invoke the designRequest.findDesign(sortorderEnum).fire() the UnsupportOperationException is thrown on the javascript console on chrome dev tools/Firebug console.
Looks like it is related to Issue 6504, which will throw an UnsupportedOperationException if it fails to find the type you are using - consider trying to change to class methods in your enum, or wait until 2.4 is released.
If you are not using anonymous enum instances, can you post more info about this error, such as where the exception is thrown from?