dooburt
dooburt

Reputation: 3060

Entity Framework throws strange 'Invalid object name' error

I'm having problems performing an insert and continually receive the error Invalid object name 'dbo.atomic.address'. As you can see within the atomic database address does exist:

enter image description here

This is the CreateAddress function. I am passing nulls into addressline3 and addressline4. I have wondered if this might be the problem. I have checked the .tt templates and the Address object doesn't have these marked as Nullable - problem?

public static int CreateAddress(string addressline1, string addressline2, string addressline3,
        string addressline4, string postcode, int cityid, int countryid, int clientid,
        string tag, bool active, string notes = null)
    {
        using (var ctx = new atomicEntities())
        {
            var a = new Address
                        {
                            ClientId = clientid,
                            AddressTag = tag,
                            AddressLine1 = addressline1,
                            AddressLine2 = addressline2,
                            AddressLine3 = addressline3,
                            AddressLine4 = addressline4,
                            CityId = cityid,
                            Postcode = postcode,
                            CountryId = countryid,
                            AddressNotes = notes,
                            Active = active,
                            Token = DateTime.UtcNow
                        };
            ctx.Connection.Open();
            ctx.Addresses.AddObject(a);
            ctx.SaveChanges();
            return a.AddressId;
        }    
    }

Just for reference, this is the error I receive with the stack:

enter image description here

Also, the data I am inserting is as follows:

enter image description here

BusinessStreet2 is empty, but I can confirm that even when populated the error still occurs.

Any help or suggestions welcome on how to make this work! :D I have read that it might be a plural or singular issue, but I can't see where I am going wrong?

EDIT Xml from Atomic.edmx:

    <EntityContainer Name="atomicModelStoreContainer">
          <EntitySet Name="address" EntityType="atomicModel.Store.address" store:Type="Tables" Schema="dbo" />
 </EntityContainer>
        <EntityType Name="address">
          <Key>
            <PropertyRef Name="address_id" />
          </Key>
          <Property Name="address_id" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
          <Property Name="client_id" Type="int" Nullable="false" />
          <Property Name="address_tag" Type="nvarchar" MaxLength="50" />
          <Property Name="address_line_1" Type="nvarchar" MaxLength="255" />
          <Property Name="address_line_2" Type="nvarchar" MaxLength="255" />
          <Property Name="address_line_3" Type="nvarchar" MaxLength="255" />
          <Property Name="address_line_4" Type="nvarchar" MaxLength="255" />
          <Property Name="city" Type="int" />
          <Property Name="postcode" Type="nvarchar" MaxLength="12" />
          <Property Name="country" Type="int" />
          <Property Name="address_notes" Type="nvarchar" MaxLength="500" />
          <Property Name="active" Type="bit" Nullable="false" />
          <Property Name="token" Type="datetime" />
        </EntityType>

Upvotes: 1

Views: 2651

Answers (2)

dooburt
dooburt

Reputation: 3060

After much faffing about, I found the problem. This turned out to be a trigger on the table for INSERT and UPDATE. Doh! The trigger was obviously untested and completely forgotten:

CREATE TRIGGER AddressTrigger 
ON [address]
FOR INSERT, UPDATE
AS
DECLARE @Address As Int;
SELECT @Address = i.[address_id] FROM inserted i;
UPDATE [dbo].[atomic].[address]
set
    [token] = GETDATE()
where [address_id] = @Address;
GO

I vaguely remember setting these up myself now, but had forgotten since. Please accept my apologies for wasting everyone's time! I'll never get the next 3 hours of my life back!

Upvotes: 2

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364249

It looks like a problem in your mapping because if your database name is Atomic the correct name of table is Atomic.dbo.Address. Also EF should not need to add database name into object name.

Upvotes: 0

Related Questions