VS 2010 Entity Repository Error - vb.net

In my project I have it set up so that all the tables in the DB has the property "id" and then I have the entity objects inherit from the EntityBase class using a repository pattern. I then set the inheritance modifier for "id" property in the dbml file o/r designer to "overrides"
Public MustInherit Class EntityBase
MustOverride Property id() As Integer
End Class
Public MustInherit Class RepositoryBase(Of T As EntityBase)
Protected _Db As New DataClasses1DataContext
Public Function GetById(ByVal Id As Integer) As T
Return (From a In _Db.GetTable(Of T)() Where a.id = Id).SingleOrDefault
End Function
End Class
Partial Public Class Entity1
Inherits EntityBase
End Class
Public Class TestRepository
Inherits RepositoryBase(Of Entity1)
End Class
the line
Return (From a In _Db.GetTable(Of T)() Where a.id = Id).SingleOrDefault
however produces the error "Class member EntityBase.id is unmapped" when i use VS 2010 using the 4.0 framework but I never received that error with the old one. Any help would be greatly appreciated. Thanks in advance.

Finally found the answer to my problem.... had to change where a.id = id to a.id.equals(id)

Related

Generic Collection of T for ICollection of DbEntities

The Situation-
Have EF database entities with collections of other database entities as navigation properties all of which are not exposed outside of the project. Each of these entities are nested in base classes that allow public read and protected set access to the Db entity's property values and are the classes the business layer will work with. So these base classes also need collections of their Db entity's navigation properties.
What I have now-
I am still working on how I want the data layer to interact with with the other layers that reference it. So I have been experimenting with creating collection classes that can be set equal to a collection of Db entities and create a collection of the base class that the Db entity is nested in. The collection is then exposed outside the class though an interface as well as the base class. The interface gives access to some custom methods of collection class like accepting DTOs and prebuilt quires but would hide any Add methods.
The Problem-
I would like to create a generic base class for the collections but cant seem to find a way. I have gotten close to something workable but it was ugly and confusing. Below is an outline of what I'm working with. The narrowing operators in the entity class work well. I would rather have widening operators and narrowing operators in the base class so I could keep the New method private for the base classes but I don't want to expose the actual database entities outside the Data Layer.
Interops Namespace referenced by all projects in solution-
Public Interface IGenericEntity
End Interface
Public Interface INavigationPropertyEntity : Inherits IGenericEntity
End Interface
Public Interface IDbEntityToExposedEntityCollection(Of TEntity As IGenericEntity)
'Used to hide Add method
End Interface
Public Interface IPublicEntity
ReadOnly Property NavProps As IDbEntityToExposedEntityCollection(Of INavigationPropertyEntity)
End Interface
Data Layer project-
-Database Entities
Friend Class DbEntity1
Public Shared Narrowing Operator CType(ByVal value As DbEntity1) As NavigationPropertyEntity
Return New NavigationPropertyEntity(value)
End Operator
End Class
Friend Class DbEntity2
Public Sub New()
NavigationPropertyDbEntities = New HashSet(Of DbEntity1)
End Sub
Public Property NavigationPropertyDbEntities As ICollection(Of DbEntity1)
Public Shared Narrowing Operator CType(ByVal value As DbEntity2) As PublicEntity
Return New PublicEntity(value)
End Operator
End Class
-Exposed Base Classes
Public Class NavigationPropertyEntity : Implements INavigationPropertyEntity
Private _value As DbEntity1
Friend Sub New(value As DbEntity1)
_value = value
End Sub
End Class
Public Class PublicEntity : Implements IPublicEntity
Dim _value As DbEntity2
Friend _NavProps As DbEntityToPublicEntityCollection(Of INavigationPropertyEntity, DbEntity1, NavigationPropertyEntity)
Public ReadOnly Property NavProps As IDbEntityToExposedEntityCollection(Of INavigationPropertyEntity) Implements IPublicEntity.NavProps
Get
Return _NavProps
End Get
End Property
Friend Sub New(value As DbEntity2)
_value = value
_NavProps = new DbEntityToPublicEntityCollection(Of INavigationPropertyEntity, DbEntity1, NavigationPropertyEntity)(_value.NavigationPropertyDbEntities)
End Sub
End Class
-Entity Specific Collection
Friend Class DbEntityToPublicEntityCollection(Of IEnt As INavigationPropertyEntity, DbEnt As DbEntity1, ExpEnt As NavigationPropertyEntity)
Inherits HashSet(Of IEnt)
Implements IDbEntityToExposedEntityCollection(Of IEnt)
Public Sub New(value As HashSet(Of DbEnt))
For Each ent In value
Dim exp As NavigationPropertyEntity = ent
Dim i As INavigationPropertyEntity = exp
MyBase.Add(i)
Next
End Sub
End Class
Additional Info Edit
The main issue is being able to have a generic collection declared something like,
Friend Class DbEntityToPublicEntityCollection(Of IEnt, DbEnt as class, ExpEnt As class)
because in the New sub ExpEnt cannot be converted to DbEnt and a TryCast will just result in a runtime error.
Like I said, I tried setting up classes that are inherited by the exposed and entity classes so a new method with a parameter for the exposed class could be set up but with trying to have the interfaces in there it got unmanageable pretty quick. Then again I have not worked much with making my own generic types before so I'm not sure if this is even a proper strategy.

DbContext type is not defined

Working with EF 6.1. I've created two table class files:
Public Class Student
Public Property StudentID() As Integer
Public Property StudentName() As String
Public Property DateOfBirth() As DateTime
End Class
Public Class Standard
Public Property StandardID() As Integer
Public Property StandardName() As String
End Class
Also created one DbContext file (SchoolContext.vb):
Imports System.Data.Entity
Namespace TestDataAccess
Public Class SchoolContext
Inherits DbContext
Public Sub New()
MyBase.New(ConfigurationManager.ConnectionStrings("dbConnString").ConnectionString)
End Sub
Private _Students As DbSet(Of Student)
Private _Standards As DbSet(Of Standard)
End Class
End Namespace
In my test page, I'm making reference to the DbContext file by using the following:
Dim context As SchoolContext = New SchoolContext
but it keeps giving the error "Type 'SchoolContext' is not defined." Even tried importing the namespace 'TestDataAccess' but still received the error. I did install EF into my project via NuGet, and it show in my packages.config file, so I know that's not the issue.
<package id="EntityFramework" version="6.1.2" targetFramework="net451" />
Any ideas what could be wrong?
I am nog very familiare with vb, but what is the scope of your DbContext.
Shouldn't _standards and _studente be public instead of private?
You need the EntityFramework.dll reference (and not System.Data.Entity).

fake EF DBContext in Visual Basic.NET

I found solution for faking DBContext in C#. Creating an Interface IDbContext. For production code I create a LifeDbContext that inherit from DbContext and implement IDbContext. For unit testing I create a class FakeDbContext that implements also IDbContext and fake the Datebase with Collections ... fine.
But now I have to do it with Visual Basic.NET. Because of I have to implicitly implement each member of the interface IDbContext I can not implement the members of DbContext base class.
In c# there is no need to explicit implement a member of an interface.
Any idea?
Here my code:
Public Interface IMyDbContext
Property Orders As IDbSet(Of Oders)
...
ReadOnly Property Database() As Database
Function SaveChanges() As Integer
ReadOnly Property Configuration As Infrastructure.DbContextConfiguration
End Interface
Public Class MyDbContext
Inherits DbContext
Implements IDbContext
Public Property Orders As IDbSet(Of Orders) Implements IMyDbContext.Oders
...
Public ReadOnly Property Database() As Database Implements ILimsDbContext.Database
Get
Return MyBase.Database
End Get
End Property
Public Function SaveChanges() As Integer Implements IMyDbContext.SaveChanges
Return MyBase.SaveChanges()
End Function
Public ReadOnly Property Configuration() As Infrastructure.DbContextConfiguration Implements IMyDbContext.Configuration
Get
Return MyBase.Configuration
End Get
End Property
End Class
What I don't like is the shadowing the already in DbContext implemented members in MyDbContext only for explicit implement the interface.
You can modify the t4 template to add your interfaces and implements clauses, but that would lock you into your customized t4 implementation. I did something along these lines in a post on adding property logging a while back. See if this post is helpful.

Can anyone spot the issue with this VB.Net code?

I'm writing some code in VB.Net which I hope demonstrate to colleagues (not to say familiarise myself a little more) with various design patterns - and I'm having an issue with the FactoryMethod Pattern.
Here's my code:
Namespace Patterns.Creational.FactoryMethod
''' <summary>
''' This is the Factory bit - the other classes are merely by way of an example...
''' </summary>
Public Class CarFactory
''' <summary>
''' CreateCar could have been declared as Shared (in other words,a Class method) - it doesn't really matter.
''' Don't worry too much about the contents of the CreateCar method - the point is that it decides which type
''' of car should be created, and then returns a new instance of that specific subclass of Car.
''' </summary>
Public Function CreateCar() As Car
Dim blnMondeoCondition As Boolean = False
Dim blnFocusCondition As Boolean = False
Dim blnFiestaCondition As Boolean = False
If blnMondeoCondition Then
Return New Mondeo()
ElseIf blnFocusCondition Then
Return New Focus()
ElseIf blnFiestaCondition Then
Return New Fiesta()
Else
Throw New ApplicationException("Unable to create a car...")
End If
End Function
End Class
Public MustInherit Class Car
Public MustOverride ReadOnly Property Price() As Decimal
End Class
Public Class Mondeo Inherits Car
Public ReadOnly Overrides Property Price() As Decimal
Get
Return 17000
End Get
End Property
End Class
Public Class Focus Inherits Car
Public ReadOnly Overrides Property Price() As Decimal
Get
Return 14000
End Get
End Property
End Class
Public Class Fiesta Inherits Car
Public ReadOnly Overrides Property Price() As Decimal
Get
Return 12000
End Get
End Property
End Class
End Namespace
When I try to compile this, I am getting errors (BC30311) in the CarFactory.CreateCar telling me that it can't convert Fiesta, Mondeo, and Focus into Car. I don't see what the issue is - they're all subclasses of Car.
Doubtless I'm overlooking something simple. Can anyone spot it?
Cheers,
Martin.
Put Inherits on new line or use : to separate the class name and Inherits statement:
Public Class Mondeo
Inherits Car
...
Public Class Focus
Inherits Car
...
Public Class Fiesta
Inherits Car
...
Your Inherits keyword must be on a new line. This is documented by Microsoft on their help and Support. http://support.microsoft.com/kb/307222
Change the SavingsAccount class
definition as follows, so that
SavingsAccount inherits from Account
(note that the Inherits keyword must
appear on a new line):
The first error in the list is just the one at the lowest line number, it's not always the actual cause of the error.
Further down in the list of errors you will see (among several others) three more errors saying End of statement expected. at each of the subclasses. This is beacuse Class and Inherits are separate statements and go on separate lines:
Public Class Mondeo
Inherits Car
or:
Public Class Mondeo : Inherits Car
When you fix these errors, the classes do actually inherit from Car, and your code works.

In VB, How do you force an inherited class to use an attribute on the class?

I'm trying to force an inherited class to use a custom attribute. I'm creating a class library where the user who wants to create an item will do so, but be forced to add an attribute (or visual studio will automatically add the default attribute) to their inherited class. Here is what I'm hoping to achieve:
BaseClass.vb:
<CustomAttribute(10)> _
Public Class BaseClass
End Class
MyClass.vb:
<CustomAttribute(12)> _
Public Class MyClass
Inherits BaseClass
Public Sub New()
Mybase.New()
End Sub
End Class
So the thought is that much like when you mark a function as "MustOverride" and then the inherited class must override the function, I want the attribute to be "MustOverride" causing the inherited class to specify the value.
I've tried this, and it will work, but it would be much cleaner if I could use attributes:
BaseClass.vb:
Public MustInherit Class BaseClass
Public MustOverride ReadOnly Property CustomAttribute() As String
End Class
MyClass.vb:
Public Class MyClass
Inherits BaseClass
Public Sub New()
Mybase.New()
End Sub
Public Overrides ReadOnly Property CustomAttribute() As String
Get
Return "testing"
End Get
End Property
End Class
Thank you for any help you can provide.
Scott
Did you consider implementing an interface instead? I assume that you're using a base class as you want to provide some code in the base, if not then an Interface might be better depending on your other requirements:
Interface IBase
ReadOnly Property CustomAttribute() As String
End Interface
It's still very compact and when you type 'Implements IBase' in a new class Visual Studio will fill in the code for you.
There's no way in .NET to force a class to define an attribute at compile time. The best you'll be able to do is check at run-time whether the attribute was defined, and if not to throw an exception.